Python Try Except
Python Try Except is used to handle exceptions thrown the by Python Interpreter at runtime. When the interpreter throws an error, the program execution stops abruptly. To avoid such havoc, we can use try except statement and handle the exceptions programmatically.
In this tutorial, we shall learn the syntax of try except and how to use try except in Python programs to handle exceptions at runtime.
Syntax of Try Except in Python
Following is the syntax of try-except statement.
try: #your code that may throw exceptions statement(s) except Exception1: #If Exception1 is thrown, then execute this block. statement(s) except Exception2: #If Exception2 is thrown, then execute this block. statement(s) else: #If there is no exception then execute this block. statement(s)
Include your code that could throw exceptions in try block.
Then except block(s) follow. You can handle multiple exceptions thrown by your code in try block. Suppose if your try block code can throw two types of exceptions, the we can have two except blocks that handle each of the exception. You can provide statements for execution separately for each type of Exception that could occur.
Else block is optional. And if you provide else block, it is executed only when try block does not throw any exception. In other words, when there is no exception, Python executes else block.
Example 1: Python Try Except
In this example, we will try to divide a number with other. When the denominator is zero, an exception is thrown by the Pyhton Interpreter and we will catch it in runtime using except block.
a = 3 b = 0 c = 0 try: c = a/b except ZeroDivisionError: print('b is zero. Correct the value or your logic.') print(c)
b is zero. Correct the value or your logic. 0
If you do not use try catch as shown in the below program you will get Exception at runtime. Specifically, ZeroDivisionError for the code.
a = 3 b = 0 c = 0 c = a/b print(c)
Traceback (most recent call last): File "example.py", line 5, in <module> c = a/b ZeroDivisionError: division by zero
Example 2: Python Try Except catching Multiple Exceptions
In this example, we will try to typecast given string values to integer, and then divide a number with other. If the string is a not a parsable integer, then we get ValueError. If the denominator is zero, then we get ZeroDivisionError. We shall write two except blocks, one for ValueError and other for ZeroDivisionError.
x = input('Enter numenator : ') y = input('Enter denomenator : ') try: a = int(x) b = int(y) c = a/b except ValueError: print('Check if input string is parsable integer') except ZeroDivisionError: print('Denomenator is zero.')
Example 3: Python Try Except with Else Block
In the syntax, we have already seen that we can provide an optional else block after try and except blocks.
In this example, we shall write a try except block with a trailing else block. This else block is executed when no except block catches an error.
We shall take the same scenario as that of in previous example.
x = input('Enter numenator : ') y = input('Enter denomenator : ') try: a = int(x) b = int(y) c = a/b except ValueError: print('Check if input string is parsable integer') except ZeroDivisionError: print('Denomenator is zero.') else: print('No Errors.')
In this tutorial of Python Examples, we learned how to use try except statement to handle exceptions in Python programs.