Python 101 : Exception handling.


To handle errors we use the "try/except/else/finally" clauses. Below is a simple illustrating their use:


The code will continue running instead of exiting after encoutering an error
The error is handled by the "try/except" clause.

That main reason behind using the "try/except/else/finally" is that our code doesn't abruptly exit after encountering an error.
The error is caught, dealt with, and the program resumes running.

Remark:

"except" without arguments will catch all types of errors.

We could also specify the errors that we want "except" to catch, for example the EOF error:

Comments

Leave as a comment:

Archive