Handling Exceptions using try and except in Python 3.x. or earlier

Like other programming languages, we can also handle exceptions in Python. In this tutorial we will learn about how we can implement exception handling in Python 3.x. or earlier. Before learning handling exceptions in Python let’s see a short description of what does exception handling mean.

Exceptions, in general, refers to some contradictions or unwanted situations. During program-development, there may be some snippets where aren’t sure about the result. There exception handling comes handy in order to avoid any errors.

Inbuilt Exceptions in Python

  • Divide by zero
  • Index out of range
  • Invalid input type
  • Opening a non-existing file, etc.

These exceptions are handled by the default exception handler present in Python 3.6 or earlier.

Exception Handling in Python 3.x. or earlier

In this, we use a pair of try and except clauses.

Syntax:        try:
                    # statements that may raise an exception
                    # handle exception here

The <try suite> is executed first; if during the course of executing the <try suite>, an exception is raised that is not handled otherwise, and
the <except suite> is executed, with <name> bound to the exception, if found; if no matching except suite is found then unnamed except suite is executed.

Python Code: Exception handling

# handling exceptions with the help of inbuilt exception handler


# handling with the help of inbuilt exeptions

except IOError:
     print "I/O error occurred"

except ValueError:
     print "Data to integer conversion failed"

except ZeroDivisionError:
     print "Division by zero"

# handling unexpected error
     print "Unexpected Error"
Output: I/O error occurred

An argument inside “Except” block

We can provide a second argument for the except block, which gives a reference to the exception object.

Syntax:      try:
                  # statements that may raise an exception
             except <Exception Name>, <exArgument>:
                  # handle exception here

The except clause can then use this additional argument to print the associated error-message of this exception as <exArgument> .message.

# try clause & except clause

    print "result of 10/5",(10/5)
    print "result of 10/0",(10/0)

except ZerDivisionError, e:
    print "Exception -",e.message  # prints the standard error message of raised expression

Finally Block in Python

The finally block is also declared along with “try”. The difference between except and finally clause is that the finally clause is executed every time the try block executes whereas the except block is executed only when try block raises an exception.

# finally block execution 

    print fh.read()

    print "Exception Occurred"

    print "Execution Completed"

Output: Exception Occurred 
        Execution Completed

I hope now you have got a clear idea of handling exceptions in Python.

Also, learn

Leave a Reply

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