Exception Handling

Errors detected during execution of a program are called exceptions. Exception handling in any programming language is same. Exception Handling

We use Try and Except Statement  to handle the exceptions in python.

In python, below is the syntax for exception handling

Exception Handling Syntax

try:
    #code

except:
    #print exception

Try and Except Statement

try: block contains one or more statements which are likely to encounter an exception. If the statements in this block are executed without an exception, the subsequent except: block is skipped.

If the exception does occur, the program flow is transferred to the except: block. The statements in the except: block are meant to handle the cause of the exception appropriately. For example, returning an appropriate error message.

The following example will throw an exception when we try to divided an integer by a string.

try:
    a=10
    b='0'
    print(a/b)
except:
    print('Some error occurred.')
Some error occurred.

else and finally

In Python, keywords else and finally can also be used along with the try and except clauses. While the except block is executed if the exception occurs inside the try block, the else block gets processed if the try block is found to be exception free.

try:
    #statements in try block
except:
    #executed when error in try block
else:
    #executed if try block is error-free
finally:
    #executed irrespective of exception occured or not

Example of try, except, else, finally

try:
    print('try block')
    a = 8
    b = 2
    c = a/b
except ZeroDivisionError:
    print("Error block")
    print("Division by 0 not accepted")
else:
    print("else block")
    print("Division = ", c)
finally:
    print("finally block")
    x=0
    y=0
print ("Out of try, except, else and finally blocks." )
try block
else block
Division =  4.0
finally block
Out of try, except, else and finally blocks.

The first run is a normal case. The out of the else and finally blocks is displayed because the try block is error-free. The second run is a case of division by zero, hence, the except block and the finally block are executed, but the else block is not executed.

In the third run case, an uncaught exception occurs. The finally block is still executed but the program terminates and does not execute the program after the finally block. Typically the finally clause is the ideal place for cleaning up the operations in a process. For example closing a file irrespective of the errors in read/write operations. This will be dealt with in the next chapter.

Advantages :

Exception handling is a programming technique used to handle errors or exceptional situations that may occur during program execution. Here are some of the advantages of using exception handling:

  • Improved program reliability: With exception handling, you can catch and handle errors before they crash your program, making your code more reliable.
  • Better code organization: Exception handling allows you to separate error-handling code from the main program logic, making your code more organized and easier to read.
  • Easier debugging: By handling exceptions properly, you can provide more detailed and meaningful error messages, making it easier to debug your code.
  • Graceful error recovery: Exception handling provides a way to recover gracefully from errors, allowing your program to continue executing even when an error occurs.
  • More robust programs: Exception handling allows you to anticipate and handle unexpected situations, making your programs more robust and able to handle a wider range of scenarios.
  • Better user experience: By handling errors properly, you can provide better feedback to the user, making your program more user-friendly.

Exception Handling
  1. Errors detected during execution of a program are called exceptions.
  2. try: block contains one or more statements which are likely to encounter an exception.
  3. except: block are meant to handle the cause of the exception appropriately .

Exception Handling Exception Handling