Python SystemExit Exception
Some Python magic around
Recently I’ve been at the interview for one big Israel startup.
They asked me to read the code below and say 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
Let’s try to figure out what is going on within this small piece of code.
Did you know that
- Lines 10–11 will raise an
SystemExit exception is caught by
except statement in line 12
Exception will be printed
sys.exit(1) in line 14 raises an exception
5. Code in
finally statement in line 16 is executed and
Out is printed out
6. And finally, the statement in line 22 is executed because of exception from
Out2 is printed
Now it’s easy to understand what will be printed (no matter what is going on in
Thanks for the attention to the topic, feel free to leave your questions in the comments for discussion.