How to Catch Error in Python

To catch an error in Python, use the try and except block. The try block contains the code that may cause an error. If the code inside the try block causes an error, we can catch these errors by using an except block. The except block catches the exception. If the exception caused and the exception class provided I the exception block matches, then that exception block is executed. There can be multiple try and except blocks.

x = 5
y = 0

   z = x / y
except ArithmeticError as e:
   print("Arithmetic Error has occurred")


Arithmetic Error has occurred
Traceback (most recent call last):
 File "/Users/krunallathiya/Desktop/Code/R/", line 5, in <module>
 z = x / y
ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
 File "/Users/krunallathiya/Desktop/Code/R/", line 9, in <module>
AttributeError: 'ZeroDivisionError' object has no attribute '__class_'

In this program, we created two variables, x, and y. Then, inside the try block, we divided x by y and assigned the value to the variable z. This causes an error called the ZeroDivisionError.

When a number is divided by zero, this error is raised. This can be solved by replacing the denominator number. This will display the error as division by error, <class ‘ZeroDivisionError’>.

We used an except statement to catch the error. We used the ArithmeticError class to catch the error. This error class catches the ZeroDivisionError.

How to catch AttributeError in Python

To catch an AttributeError in Python, use the try-except block.

num = 20

except AttributeError:
  print("Attribute append does not exist for this type of data")


Attribute append does not exist for this type of data

In this program, we created an integer variable. We have assigned the value as 20. Inside the try block, we append 2 to the num variable. Generally, the append() function in Python appends the data to the list.

But integer number does not have a method called append. Hence this error is raised. Hence we have used an AttributeError class for catching this error. This class catches the attribute error.

num = 1.75

  for i in num:
    print(i, end=" ")
except TypeError:
  print("TypeError has occurred")


TypeError has occurred

In this program, we have created a variable for storing a floating-point number. We have assigned the value as 1.75 in the variable named num. Then inside the try block, we traverse over the num variable.

Basically, we can iterate over the string or list using the for loop. We cannot iterate over the floating-point number. We can use the numbers as a range but in this program, we have not used a range instead we are iterating over the floating-point number. Hence, the TypeError is raised because the floating-point number does not support iteration.

We can catch this type of error by using a TypeError class. We used this class in this program hence the except block catches this error and displays the error message as output.

That’s it for this tutorial.

Related posts

How to Solve FloatingPointError in Python

How to Solve MemoryError in Python

How to Solve EOFError in Python

Leave a Comment