Exception Handling In Python

Exception Handling:

  • An exception is an abnormal condition that occurs during the execution of a computer program.
  • Programming languages provide various mechanisms to handle exceptions. They include,
    1. Checking of return value of a function
    2. Checking of a global variable or getting error code from a global function and doing a lookup for its description
    3. Explicit implementation of exception class hierarchies and exception handlers
  • Here the mechanism being explained is the third one from the above list of exception handling mechanisms.

Exception Handling in Python:              

  • Python has built-in support for exception handling through Exception class hierarchy and the keywords try, except, finally and raise statements.
  • The try keyword begins a block of Python statements for which exception handling is to be provided.
  • The try block is followed by the, except block, which has the code for handling the exception.
  • A try block can have one or more except blocks, each block handling one or a group of exceptions.
  • A try block can have an optional finally block for cleaning up of resources.
  • The finally block is executed regardless of whether an exception has happened inside the try block.
  • A Python program can create exceptions and raise them through the raise statement.

What happens when try and except are not used:

  • When exceptions are not handled using try, except and finally clauses, the Python Program prints the traceback along with the exception information after which the program terminates.

Example: Without Exception Handling:

def convert(number, factor):

        return number/factor;

   

number  = 10;

factor  = 0;

 

val = convert(10, 0);

print(val)

 

Output:

Traceback (most recent call last):

  File "tryex0.py", line 7, in <module>

    val = convert(10, 0);

  File "tryex0.py", line 2, in convert

    return number/factor;

ZeroDivisionError: division by zero

 

What happens when try and except are used:

  • When a block of code is enclosed inside a try block and one or more except classes are provided, the except block corresponding to the raised exception handles it after which the program continues its normal execution path.
  • While a Python Program can continue after an exception it is up to the programmer to design the program so as to handle the exceptions appropriately based on their severity. For example, while processing millions of records from an external source, divide by zero on a field of a specific record can be reported in a log file and the program can continue processing other records. However, when there are severe exceptions like I/O failures most of the times it is not right to proceed further. At such circumstances the exception handler(s) can be used to report the exception in the log file and exit the program.

Example – A simple try/except block:

In the following python program the convert() function generates an exception ZeroDivisionError, which is handled by the, except handler.

 

import time

def convert(number, factor):
    try:
        return number/factor;
    except Exception as ex:
        print("%s:Exception:%s."%(time.time(), ex))
    
number  = 10;
factor  = 0;

val = convert(10, 0);
print("Moving forward after an exception has been handled.");

if val is not None:
    print("Obtained value:%d"%(val))

val = convert(10, 1);
if val is not None:
    print("Obtained value:%d"%(val))

 

Output:

1557238256.166024:Exception:division by zero.

Moving forward after an exception has been handled.

Obtained value:10

 

Example – try block with multiple except blocks:

import time

def addToFile(fileName, line):

    try:

        file = open("/logs/CustomLog.log", "r");

        file.write(line);

 

    except FileNotFoundError as Ex:

        print("Exception Handler1: %s"%(Ex));

    except Exception as Ex:

        print("Exception Handler2: %s"%(Ex));

 

logFileName     = "/logs/CustomLog.log";

msg             = "%s:Started running"%(time.time());

 

addToFile(logFileName, msg);

print("Moving forward after a possible exception has been handled.");

 

Output – Scenario 1 - FileNotFoundError:

The program behavior when the file is not available is given here.

Exception Handler1: [Errno 2] No such file or directory: '/logs/CustomLog.log'

Moving forward after a possible exception has been handled.

 

Output – Scenario 2 - Other Exceptions:

When the file is available but the program has a defect that it has not opened it with write mode, the behavior is given here.

Exception Handler2: not writable

Moving forward after a possible exception has been handled.

 

Lookup for the right exception handler:

When an exception occurs, Python searches for a matching exception handler. If a handler of appropriate type is not found in the current scope the search is continued in the outer scopes till an exception handler is found. If a handler is not found then the program will exit after printing the traceback. If a handler is found, then the handler will be executed and the program will continue after the try block.

Exception Handling In Python


Copyright 2019 © pythontic.com