Home ProgrammingPythonPython Files & Exceptions Python Exception Handling : How to prevent Program Crashes

Python Exception Handling : How to prevent Program Crashes

by WhereWhatHow
Python Exception Handling

In the last tutorial, we learned about Python exceptions. We know that exceptions abnormally terminate the execution of a program. This is why it is important to do Python Exception Handling. In Python, we use the try...except block

Python try…except Block

The try...except block is used to handle exceptions in Python. Here’s the syntax of try...except block:

    # code that may cause exception
    # code to run when exception occurs

Here, we have placed the code that might generate an exception inside the try block. Every try block is followed by an except block.

When an exception occurs, it is caught by the except block. The except block cannot be used without the try block.

Example: Python Exception Handling Using try…except

    numerator = 10
    denominator = 0

    result = numerator/denominator

    print("Error: Denominator cannot be 0.")

# Output: Error: Denominator cannot be 0. 

In the example, we are trying to divide a number by 0. Here, this code generates an exception.

To handle the exception, we have put the code, result = numerator/denominator inside the try block. Now when an exception occurs, the rest of the code inside the try block is skipped.

The except block catches the exception and statements inside the except block are executed.

If none of the statements in the try block generates an exception, the except block is skipped.

Catching Specific Exceptions in Python

For each try block, there can be zero or more except blocks. Multiple except blocks allow us to handle each exception differently.

The argument type of each except block indicates the type of exception that can be handled by it. For example,

    even_numbers = [2,4,6,8]

except ZeroDivisionError:
    print("Denominator cannot be 0.")
except IndexError:
    print("Index Out of Bound.")

# Output: Index Out of Bound

In this example, we have created a list named even_numbers.

Since the list index starts from 0, the last element of the list is at index 3. Notice the statement,


Here, we are trying to access a value to the index 5. Hence, IndexError exception occurs.

When the IndexError exception occurs in the try block,

  • The ZeroDivisionError exception is skipped.
  • The set of code inside the IndexError exception is executed.

Python try with else clause

In some situations, we might want to run a certain block of code if the code block inside try runs without any errors.

For these cases, you can use the optional else keyword with the try statement.

Let’s look at an example:

# program to print the reciprocal of even numbers

    num = int(input("Enter a number: "))
    assert num % 2 == 0
    print("Not an even number!")
    reciprocal = 1/num


If we pass an odd number:

Enter a number: 1
Not an even number!

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

Enter a number: 4

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

Enter a number: 0
Traceback (most recent call last):
  File "<string>", line 7, in <module>
    reciprocal = 1/num
ZeroDivisionError: division by zero


Exceptions in the else clause are not handled by the preceding except clauses.

Python Exception Handling : try…finally

In Python, the finally block is always executed no matter whether there is an exception or not.

The finally block is optional. And, for each try block, there can be only one finally block.

Let’s see an example,

    numerator = 10
    denominator = 0

    result = numerator/denominator

    print("Error: Denominator cannot be 0.")
    print("This is finally block.")


Error: Denominator cannot be 0.
This is finally block.

In the above example, we are dividing a number by 0 inside the try block. Here, this code generates an exception.

The exception is caught by the except block. And, then the finally block is executed.

Python Exception Handling

Related Posts

Leave a Comment