Python Try Except Tutorial

Python provides the try , except and finally statements in order to catch exceptions and manage them properly. The exception management generally named as try and except for Python terminology. In this tutorial, we examine how to catch manage and execute reliable code for Python errors and exceptions.

try, catch, else and finally Syntax

The try , catch , else and finally keywords have following syntax. The try statement is a must but other statements are optional and can be used according to the situation.

try:
   CODE
except:
   CODE
else:
   CODE
finally:
   CODE

Catch Exceptions with try and except

The most basic example about try and catch is only using the try and except statement without other statements like else , finally . The try statement is used to detect the exception where it is used as a code block and all code that can create exception is located inside the try statement code block. The exception statement is used to catch exceptions and execute code after exception. In the following example, we try to use an undefined variable that creates NameError: name 'x' is not defined exception. But we will catch this exception with the except statement and print “This is an undefined variable exception” message.

try:
  print(x)
except:
  print("This is an undefined variable exception")
Catch Exceptions with try and except

Catch Named Exceptions

A code block may throw different type of exceptions which are related with different causes. The named exceptions are used to only catch specific exception type. Only the specified exception type it catched and other exceptions are just skipped. For example if a variable is not defined and try to be used the NameError exception is thrown. We can use except NameError statement to catch only NameError type exceptions.

try:
  print(x)
except NameError:
  print("This is an undefined variable exception")
Catch Named Exceptions

Catch Multiple Exceptions with except

Multiple exceptions can be caught by using the except statement. Just defined new except block after the last except. In the following example, we define 3 except block which matches a different type of exceptions like NameError, ZeroDivisionError, and default exception.

try:
  print(x)
except NameError:
  print("This is an undefined variable exception")
except ZeroDivisionError:
  print("This is an zero division exception")
except:
  print("An exception is occured")
Catch Multiple Exceptions with except

Alternative the zero division exception can be tested in the following example.

try:
  print(1/0)
except NameError:
  print("This is an undefined variable exception")
except ZeroDivisionError:
  print("This is an zero division exception")
except:
  print("An exception is occured")

Run Code with else If There Is No Exceptions

The try and except statements also provides the else statement. If there is no exception we can executed specific code block by using the else statement after except blocks. In the following code if there is not exception the message “There is no exception” is printed to the screen.

try:
  print("Everything is OK")
except:
  print("An exception is occured")
else:
  print("There is no exception")
Run Code with else If There Is No Exceptions

Run Code Regardless of Exception with finally

Another useful feature of the try and except statement is using the finally statement which is used to execute code if there is an exception. The finally statement has a code bloock which is executed if there is an exception in the try code block.

try:
  print(1/0)
except:
  print("An exception is occured")
finally:
  print("We have solved exception related problems")
Run Code Regardless of Exception with finally

Throw Exception with raise Statement

The try and except statements are used to catch when an exception occurs. The exception is automatically raised by Python interpreter inside the try statement block. This way of raising exception is called implicit exception raising. Alternatively we can rais exceptions explicitly by using the raise keyword and the exception type with message. The exceptions are defined as objects and accepts string parameters in order to get message about exception.

x = 5

raise Exception("I don't like x value")

Alternatively we can raise explicit exception side the try and except block like below.

try:
  x=5
  if x == 5:
    raise Except("I don't like x value")
except:
  print("An exception is occured")
Raise Exception with throw

Leave a Comment