Python | Tutorial 7 | Exception – Part 1

Posted on Posted in Software

In the previous tutorial, we discussed user defined function, using which you can create your own function and use it accordingly in your program. In this part 1 of 2 tutorials, we will be covering the basics of exception. Let’s get started.

If you have studied C++ or Java, then you must read about the exception. Those of you who haven’t, well it is pretty simple. Exception occurs whenever there is an error in code or input and then the program immediately stops executing. For example, if you divide 3 by 0, then you will get the error known as ZeroDivisionError, which is pretty obvious.

The interesting thing is that different exceptions are raised for different reasons. For instance, NameError when an unknown or undefined variable is used, SyntaxError when you do not write the correct syntax, ImportError when importing fails, etc. Watch the video to understand.

So how do we handle an exception? We can use the try/except statement. In the try block, you write the main code that might generate an error or technically exception. If exception occurs, then code in the try block stops getting executed and the code in the except block runs. If no exception, then except block won’t run.

Taking that ZeroDivisionError example, we have modified a little bit using try/except block.

try:
  x=3
  y=0
  print x/y
except ZeroDivisionError:
 print(“Error: Division by Zero Not Possible”)

Output

Error: Division by Zero Not Possible

Since it will generate an error, the code in except block will get executed which, as you can see, is just a simple print statements in this case.

It doesn’t end here. A try block can have multiple errors, for which one can use different except blocks in a single try block. Multiple exceptions can also be put into a single except block using parenthesis; in this way, it will be easier for the except block to handle all of them.

try:
  x=3
  y=0
  print(x+”BE”)
  print x/y
  print x/2
except ZeroDivisionError:
 print(“Error: Division by Zero Not Possible”)
except (ValueError, TypeError):
  print(“Error!”)

Output

Error!

As you can see, since there is an exception getting generated in the first print statement, further execution of try statements is not taking place. If we interchange the position of print statements, then this is what happens.

try:
  x=3
  y=0
  print x/y
  print(x+”BE”)
  print x/2
except ZeroDivisionError:
 print(“Error: Division by Zero Not Possible”)
except (ValueError, TypeError):
  print(“Error!”)

Output

Error: Division by Zero Not Possible

Now you can see ZeroDivisionError is taking place, stopping further execution of try statements and not showing the exception corresponding to the next print statement.

Lastly, except statement without any exception specified will catch all possible errors. In general, exception handling is useful whenever there is any sort of user input associated with it.

With this, I conclude the 1st part of exception in Python tutorial series. Join us for the part 2 that I will be uploading it pretty soon. Till then, don’t forget to subscribe to my channel and hit the bell icon so that you don’t miss the next tutorial. Peace. 🙂

Spread it!

Leave a Reply