try except in Python

try except in Python

try except in Python is used for handling exceptions which are created when interpreter is executing a program and an error or an exception occurs which are handled by stopping the program execution and displaying an error message. Handling such situation is classified as exception or error handling and Python majorly uses three keywords for this purpose which are:

 

  • try – It is a block of code which is considered to be able to generate exceptions.
  • except – It is a block of code which is executed when error occurs and handles it.
  • finally – It is a block of code which is executed regardless of the try-block result which implies that even if an error or an exception does not occur, finally block will executed nonetheless.

 

 

try except block in Python

except block is written after try block and executes in the same order if an exception occurs. Syntax of try except block is given below:

try except block in Python syntax:

try:
  statements
except:
  statements

try except block example in Python:

# Python program to illustrate try except block

try:
  var = var + 1
  print(var)
except:
  print("An error occurred.")

Output:

An error occurred.

 

 

many except blocks

Instead of having one except block, a program can have multiple except blocks after the try block. Many except blocks are provided in the program to handle different type of exceptions. Syntax of many except blocks is given below:

many blocks syntax:

try:
  statements
except error_type_1:
  statements
except error_type_2:
  statements
...
...
except:
  statements

many blocks example:

# Python program to illustrate many except blocks 

try:
  numerator = 1
  denominator = 0
  numerator = numerator / denominator
  print(numerator)
except ZeroDivisionError:
  print("Zero Division Error occurred.")
except (ValueError, TypeError):
  print("Value/Type Error occurred.")
except:
  print("An error occurred.")

Output:

Zero Division Error occurred.

 

 

except block with else

Python allows the use of else statement with try except blocks which is executed if no error occurs in the try block. Syntax of try except block with else is given below:

try except block with else syntax:

try:
  statements
except:
  statements
else:
  statements

try except block with else example:

# Python program to illustrate try except block with else

try:
  numerator = 14
  denominator = 2
  numerator = numerator / denominator 
  print(numerator)
except ZeroDivisionError:
  print("Zero Division Error occurred.")
except:
  print("An error occurred.")
else:
  print("No error has occurred.")

Output:

7.0
No error has occurred.

 

 

finally block 

finally block contains that piece of code which is executed regardless of the result of try except blocks. It is placed immediately after the try except block. Syntax of finally block is given below:

finally block syntax:

try:
  statements
except:
  statements
else:
  statements
finally:
  statements

finally block example:

# Python program to illustrate finally block

File = None
try:
  File = open("math_example.txt")
  numerator = File.readline()
  denominator = File.readline()
  numerator = numerator/denominator
  print(numerator)
except:
  print("An error occurred.")
finally:
  if File == None:
    print("File does not exist.")
  else:
    print("File has been closed.")
    File.close()

Output:

An error occurred.
File does not exist.

 

Share this post

Leave a Reply

Your email address will not be published.