Introduction to Python SystemExit
In Python, SystemExit is an exception that is raised by the sys.exit() method. In general, all exceptions must be derived from BaseException which are instances of a class. The SystemExit exception is inherited from this BaseException instead of Exception or StandardError as it is not any technical error so that it can be caught by the code that catches the exception. So when this exception is raised and if it cannot be handled then the Python interpreter exits. This exception takes an argument that is passed to constructor same as the argument passed to sys.exit() function and it returns values where if it is an integer value then it specifies the system exit status, and the exit status is zero when the specified value is none and exit status will be one when it has another type of value.
How does Python SystemExit work?
Let us study Working of SystemExit:
In Python, we saw SystemExit is not a subclass of exception class instead BaseException class is a base class of SystemExit. This exception is inherited from BaseException so that it is not caught by the code that catches the exception.
In this article, SystemExit is an exception which is an exception that occurs when we want to stop the program execution and raise an error and to do this process in Python there is a built-in function to raise such exception and it is known as sys.exit() which can be used to exit from your Python program which gives a return code to the system. This sys.exit() function takes an argument (integer recommended) which gives exit status and zero or none is considered as successful termination of the program.
Examples to Implement Python SystemExit
Let us demonstrate this in detail with examples in the below section:
print("Program that uses BaseException as base class.") try: raise SystemExit except BaseException: print("Specifying BaseException in this block works.")
Explanation: So the above code uses BaseException in except block to catch the SystemExit exception but instead we can directly specify this exception and this can be demonstrated as below.
print("Program that uses SystemExit as Exception instead of BaseException.") try: raise SystemExit except SystemExit: print("Specifying SystemError exception in this block works.")
Explanation: So in the above, we can see in except block we are declaring SystemExit instead of BaseException in except block. So by this, we can conclude that both codes can be used to handle such exceptions. In Python, SystemExit Exception is raised when sys.exit() function is called because the call to this function converts to SystemError exception to execute handlers and debug a script without running the risk of losing control. When this function is executed in python program this exception when raised which means if it is not handled then Python interpreter exits the Python program without any traceback message of error is printed in the output. Let us see this in the below program
import sys print("Program to demonstrate sys.exit() function") limit = 15 if limit < 18: sys.exit("Numerical limit less than 18") else: print("Numerical limit is not less than 18")
Explanation: In the above program, we can see when the limit variable declared in the program is less than 18 then the program exits using sys.exit() function and it will not print any message or traceback error of SystemError exception instead it directly exits the program. And if the limit is above 18 then the print message is printed in the output screen. That can be seen in the below screenshot.
In the above screenshot, we can see the limit is above 18, so the print message is printed as seen above.
This SystemExit exception is not raised by sys.exit() function but also raised by two other functions in Python. The other two functions in Python are os._exit() function which exits immediately but it does not clean up and this requires one int argument.
Another exit function that raises this exception is defined by the site.py program which is an instance of Quitter class which also closes sys.stdin prior to raising this exception as SystemExit and therefore this exit function is mostly used in the REPL.
Among all the above three exit function which raises this SystemExit exception, only sys.exit() function is recommended as it is faster than the other two because SystemExit, when raised, produces smaller bytecodes and when we use os._exit() function this also exits very fast but it cannot do the cleanup as sys.exit() function.
In this article, we conclude that SystemExit is an exception that is raised by exit functions in Python. In this article, we saw that the most used function for terminating or stopping the execution of the Python program is sys.exit(). In this, we also saw how to use sys.exit() function which raises SystemExit exception without any traceback message printing on the output screen.
In Python, there are many exit functions which can be used in stopping the execution of the program such as quit(), sys.exit(), os._exit(), etc but among these sys.exit() and quit() exit functions raises SystemExit exception to exit the program.
This is a guide to Python SystemExit. Here we discuss an introduction to Python SystemExit, how does it work with programming examples. You can also go through our other related articles to learn more –