Python Exception Handling Using try, except and finally Statement



Exceptions in Python

In Python, Built-in exceptions can be raised when a program encounters an error.

When these exceptions happen, the Python interpreter holds the current process and passes it to the calling process until it is handled. If not handled, the program will crash.

For instance, let us consider a program where we have a function X that calls function Y, which in turn calls function Z. If an exception happens in function Z but is not handled in Z, the exception passes to Y and then X.

If an exception is never handled, an error message is displayed, and the program comes to a sudden unexpected stop.


Catching Exceptions in Python

Exceptions can be handled using a try statement.

The critical part that can raise an exception is placed inside the try clause. The code that handles the exceptions is written in the except clause.

After we caught the exception, we can choose what operations to perform.

Let us consider the following example:

import sys

inputList = [0, 'c', 5]

for entry in inputList:
    try:
        print("The entry is", entry)
        x = 1/int(entry)
    except:
        print("An error!", sys.exc_info()[0], "occurred.")
        print("Next entry.")
        print()
print("The reciprocal of", entry, "is", x)

Output

The entry is 0
An error! <class 'ZeroDivisionError'> occurred.
Next entry.

The entry is c
An error! <class 'ValueError'> occurred.
Next entry.

The entry is 5
The result of 5 is 0.2

In the above code, we loop through the values of the inputList list. The portion that can cause an exception is placed inside the try block.

If no exception happens, the except block is skipped, and normal flow continues. If any exception occurs, it is caught by the except block (In our example is the first and second values).

We print the name of the exception using the exc_info() function of the sys module. As we can see above, entry 0 causes ZeroDivisionError, entry c causes ValueError.

In Python, every exception inherits from the base Exception class so that we can perform the above code in the following manner:

inputList = [0, 'c', 5]

for entry in inputList:
    try:
        print("The entry is", entry)
        x = 1/int(entry)
    except Exception as e:
        print("An error!", e.__class__, "occurred.")
        print("Next entry.")
        print()
print("The reciprocal of", entry, "is", x)

The output of this program is the same output as the program above.


Catching Specific Exceptions in Python

Python offers the possibility to specify which exceptions an except clause should catch.

A try clause can have any number of except clauses to handle various exceptions, but only one will be executed if an exception occurs.

We can also use a tuple of values to specify multiple exceptions in an except clause.

In the following example, a pseudo-code of how to use multiple except clauses with the same try clause.

try:
    # do something
    pass
except TypeError:
    # handle TypeError exception
    pass
except (ValueError, ZeroDivisionError):
    # handle multiple exceptions
    # ValueError and ZeroDivisionError
    pass
except:
    # handle all other exceptions
    pass

Note: It is a good programming practice to use multiple except clauses to catch different exceptions to handle every case differently.


Raising Exceptions in Python

Exceptions are raised when errors occur at runtime. But we can also manually raise exceptions using the raise keyword.

We can optionally pass values to the exception to explain why that exception was raised.

>>> raise MemoryError
Traceback (most recent call last)
...
MemoryError: 

>>> raise KeyboardInterrupt("a message")
Traceback (most recent call last)
...
KeyboardInterrupt: a message
try:
    x = int(input("Please enter a positive integer: ")) 
    if x <= 0:
        raise ValueError("This is not a positive number.")
except ValueError as e:
    print(e)

Please enter a positive integer: -5
This is not a positive number.

Python try with else clause

In some cases, we might need to run a certain block of code if the code inside the try clause ran without any errors. For these cases, we can use the optional else keyword with the try statement.

Be cautious that exceptions in the else clause are not handled by the preceding except clauses.

Let us consider the following example:

try:
    n = int(input("Enter a number: "))
    assert n % 2 == 0
except:
    print("Not an even number!")
else:
    reciprocal = 1/n
    print("The reciprocal of", n ,"is", reciprocal)

Output

If we pass an even number, the reciprocal is computed and displayed.

Enter a number: 4 
The reciprocal of 4 is 0.25

If we pass an odd number:

Enter a number: 7 
Not an odd number!

However, if we pass 0, we get ZeroDivisionError as the code block inside else is not handled by preceding except.

Traceback (most recent call last)
----> 7     reciprocal = 1/n
ZeroDivisionError: division by zero

Python try...finally

In Python, the try statement can have an optional finally clause.

The finally clause is executed in all cases, and it is generally used to release external resources.

For instance, we may be working with a file or connected to a remote data center through the network.

In all these cases, we need to clean up the resource before the program ends, whether it is a successful ran or not. These actions (closing a file, disconnecting from the network) are recommended to be done in the finally clause to ensure the execution.

In the following, an example of using finally clause with file operations.

try:
    f = open("demo.txt", encoding = "utf-8")
    # perform fil operations
finally:
    f.close()

As we can see above, when using the finally clause, we make sure that the file is closed even if an exception happens during the program execution.



ExpectoCode is optimized for learning. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2020-2021 by ExpectoCode. All Rights Reserved.