Python SystemExit Exception Explained
Some Pythonic thing around sys.exit() function.
So I will explain this with the help of example. Read the code below and think about what will be in the output.
What do you think to be the output after running this code snippet?
Is it depend on the body of some_func function?
Let’s try to figure out what is going on within this small piece of code.
- Lines 9–10 will raise an SystemExit exception
- SystemExit exception is caught by except statement in line 11
- Exception Occurred will be printed
- sys.exit(1) in line 13 raises an exception
- Code in finally statement in line 15 is executed and Out is printed out
- And finally, the statement in line 21 is executed because of exception from sys.exit(1) and Out2 is printed
Now it’s easy to understand what will be printed (no matter what is going on in some_func):
Learn Python programming from scratch through Python programming certification course. This Python Course will help you master important Python programming concepts such as data & file operations in Python, object-oriented concepts in Python & various Python libraries such as Pandas, Numpy, Matplotlib, and so on. This Python certification course is also a gateway towards your Data Science career. The course is curated by industry experts which includes real-time case studies. Enroll & Get Certified now!