How to handle exceptions in python?

In Python, exceptions can be caught using a try-except block.

Example:


  try:
      x = 1 / 0
  except ZeroDivisionError:
      print('Division by zero!')
  

Solution:

The try block contains the code that might raise an exception. If that exception is raised, the code in the corresponding except block will be executed.

Beginner's Guide to Python