Python | Tutorial 8 | Exception – Part 2

Posted on Posted in Software

In the previous tutorial, we discussed part 1 of our exception tutorial where we told what is an exception and how to handle it. In this final part, we will be covering the additional sections regarding the exception. Let’s get started.


Till now, we came to know that while performing exception handling, your program terminates whenever you encounter any sort of error. Now, in order to ensure some code runs no matter what errors occur, you can use a finally statement.

  print(1 / 0)
except ZeroDivisionError:
  print(“Divided by zero”)
  print(“This code will run for sure! Count on it.”)


Divided by zero
This code will run for sure! Count on it.

The finally statement is placed at the bottom of a try/except statement. So the code within a finally block will always run after the execution of the code in the try block and possibly except block if an exception occurs. Run this code and check the output.

  print(4 / 0)
except ZeroDivisionError:
  print(“This is horrible”)

The code in a finally statement even runs if an uncaught exception occurs in one of the preceding blocks. So it’s like running this particular block of code mandatory. Since unknown_var is undefined but no such except block was created for it, the error is showed after writing finally statement.

Then, you can even raise exceptions by using the raise statement. Exceptions can be raised with arguments that give detail about them. In except blocks, the raise statement can be used without arguments to re-raise whatever exception occurred. So here, I haven’t used any argument but raise understood that it is a ZeroDivisionError.

  num = 5 / 0
  print num
  print(“An error occurred”)

An exception can have an argument, which is a value that gives additional information about the problem. The contents of the argument vary by exception.

If you write the code to handle a single exception, you can have a variable following the name of the exception in except statement. If you are trapping multiple exceptions, you can have a variable following the tuple of the exception. Tuple? Well, don’t worry. This will be my topic in the next tutorial. Till now, run the following code.

def temp_convrt(var):
    return int(var)
  except ValueError, arg:
    print “The argument does not contain numbers\n”, arg

temp_convrt(“Pokemon XYZ”);

This variable receives the value of the exception mostly containing the cause of the exception. The variable can receive a single value or multiple values in the form of a tuple.

Let’s conclude today’s tutorial with the assertion. It is a sanity-check that you can turn on or turn off when you have finished testing the program. An expression is tested, and if the result comes up false, an exception is raised. Assertions are carried out through the use of the assert statement.

assert 1+3==4

So here, we are testing whether 1+3 is equal to 4. Since it is true, the print is getting executed. Else we would have obtained the error.

Assert can take a second argument that is passed to the AssertionError raised if the assertion fails. Hence we wrote it is very expensive along with assertion error separated by a comma.

cost = 500
assert (cost<200), “It is very expensive!”

With this, I complete the exception section in Python. I hope you have understood and enjoyed this video. If yes, then don’t forget to leave a thumbs up and do subscribe for the upcoming videos. Follow us on Facebook and tweeter for more updates. Peace. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *