Exit with Error Messages
Generally, when a Python program encounters an error, it displays it on the console screen. But sometimes we are interested in exiting the application while displaying some text denoting a possible error which might have occurred. This process could also be used to exit the program and display some text at the end. In the following code, we will be exiting the python program after displaying some text.
Here, a string or an integer could be provided as an argument to the exit() function. If the argument is a string (denoting an error message etc.), then it will be outputted after program execution. If it is an integer, then it should be an POSIX exit code.
Exiting Python code with custom error message
In this example the below code prints “Hello world!” to the console and then exits with an error message “__PUT_ERROR_MSG_HERE__”.
Python3
print ( "Hello world!" ) exit( "__PUT_ERROR_MSG_HERE__" ) |
Output:
Hello world!
__PUT_ERROR_MSG_HERE__
How to Exit a Python script?
In this article, we are going to see How to Exit Python Script.
Exiting a Python script refers to the termination of an active Python process. In this article, we will take a look at exiting a Python program, performing a task before exiting the program, and exiting the program while displaying a custom (error) message.
Contact Us