An error or unexpected event that that occurs while a program is running is called an exception. Thanks to the elements that we will see in just a moment, we can avoid terminating the program abruptly when this occurs.
Let’s see the types of exceptions in Python and how we can handle them. Common Exceptions in Python This is a list of common exceptions in Python and why they occur: ZeroDivisionError: raised when the second argument of a division or modulo operation is zero.
IndexError: raised when we try to use an invalid index to access an element of a sequence.
KeyError: raised when we try to access a key-value pair that doesn’t exist because the key is not in the dictionary.
NameError: raised when we use a variable that has not been defined previously.
RecursionError: raised when the interpreter detects that the maximum recursion depth is exceeded. This usually occurs when the process never reaches the base case. In the example below, we will get a
. The
function is implemented recursively but the argument passed to the recursive call is
instead of
. Unless the value is already
or
, the base case will not be reached because the argument is not being decremented, so the process will continue and we will get this error.
/
in Python We can use try/except in Python to catch the exceptions when they occur and handle them appropriately. This way, the problem can terminate appropriately or even recover from the exception. This is the basic syntax:
For example, if we take user input to access an element in a list, the input might not be a valid index, so an exception could be raised:
If we enter an invalid value like 15, the output will be:
Because the
clause runs. However, if the value is valid, the code in
will run as expected. Here we have another example:
The output is:
How to Catch a Specific Type of Exception in Python Instead of catching and handling all possible exceptions that could occur in the
clause, we could catch and handle a specific type of exception. We just need to specify the type of the exception after the
keyword:
For example:
How to Assign a Name to the Exception Object in Python We can specify a name for the exception object by assigning it to a variable that we can use in the
clause. This will let us access its description and attributes. We only need to add
, like this:
For example:
This is the output if we enter
as the index:
This is another example:
This is the output if we enter the value
for
:
/
/
in Python We can add an
clause to this structure after
if we want to choose what happens when no exceptions occur during the execution of the
clause:
For example:
If we enter the values
and
for
and
respectively, the output is:
But if both values are valid, for example
and
for
and
respectively, the
clause runs after
is completed and we see:
/
/
/
in Python We can also add a
clause if we need to run code that should always run, even if an exception is raised in
. For example:
If both values are valid, the output is the result of the division and: