7. Exception Handling¶
Once in a while you may come across Exception.
Per definition an exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.
For instance if you try to read data from a file, which does not exist, a
However, you don’t want your program to crash and provide an alternative if the file does not exist.
There exist two approaches to accomplish that.
First of all you could check, whether the file exists, and if so, open it.
This is the recommended way.
However, you could also catch the
FileNotFoundError exception if it occurs and handle it properly using a
Python tries to execute the statements contained in the
If the execution fails/is interrupted by an exception, the
try-block is left and the following
except-block (if existent) is entered.
try: f = open("some_non-existent_file.txt") except FileNotFoundError: print("A FileNotFoundError occured!") # do something to handle it properly
If you don’t specify an exception name like
FileNotFoundError and leave it empty, all types of exceptions will be catched.
However, this is not recommended as it silences all exceptions!
You can add an
else-clause to a
try-statement as well.
Note, that this is optional.
else-clause will be executed if no exception occured.
Last but not least a
finally-clause can be added to the
Again, this is optional.
finally-clause is always executed at the end of a
try-statement (so after the
You can use it to ensure, that a certain value is always present or an opened file is always closed afterwards.