Updated June 17, 2023
Introduction to Python SystemExit
In Python, SystemExit is an exception the sys.exit() method raises. 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 it can be caught by the code that catches the exception. So when this exception is raised, and it cannot be handled, then the Python interpreter exits. This exception takes an argument passed to the constructor like the idea passed to the 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 the Working of SystemExit:
In Python, we saw SystemExit is not a subclass of the 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 it.
In this article, we learn about the SystemExit exception in Python, which is raised when we want to stop the execution of a program and raise an error. To achieve this, Python provides a built-in function called sys.exit(). This function allows us to exit our Python program and provides a return code to the system. When using sys.exit(), we pass an argument (typically an integer) that represents the exit status of the program. A return code of zero or None is considered as a successful termination of the program.
Examples to Implement Python SystemExit
Let us demonstrate this in detail with examples in the below section:
Example #1
Code:
print("Program that uses BaseException as base class.")
try:
raise SystemExit
except BaseException:
print("Specifying BaseException in this block works.")
Output:
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 below.
Example #2
Code:
print("Program that uses SystemExit as Exception instead of BaseException.")
try:
raise SystemExit
except SystemExit:
print("Specifying SystemError exception in this block works.")
Output:
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 a Python program, this exception when raised, which means if it is not handled, then the Python interpreter exits the Python program without any traceback message of error printed in the output. Let us see this in the below program.
Example #3
Code:
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")
Output:
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 on the output screen. That can be seen in the below screenshot.
This SystemExit exception is not raised by the sys.exit() function but is 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.
Conclusion
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 the SystemExit exception without any traceback message printing on the output screen.
Recommended Articles
This is a guide to Python SystemExit. Here we discuss an introduction to Python SystemExit, and how it works with programming examples. You can also go through our other related articles to learn more –