Exception Handling Try Except Else Finally

In this class, we discuss Exception Handling Try Except Else Finally in python.

For Complete YouTube Video: Click Here

Exception Handling Try Except

The reader should have prior knowledge of exceptions in python. Click here.

Take an example and understand the concept of Exception handling.

a=int(input("enter first number"))
b=int(input("enter second number"))
try:
    c=a/b
except:
    b+=1
    c=a/b
    print(c)

Output:
enter first number12
enter second number0
12.0

In the above program c=a/b, this is the block of code where there is a chance of getting an Exception.

The block of code that has a chance of getting the error is placed in the Try block.

If an exception occurred during the execution, the Try statement would send the Exception to Except block.

The code to handle the exception is written in Except block.

By using Try and Except, we can do exception handling.

In our previous class, we discussed whenever Exceptions arise. The program stops its execution.

In the above program, the user has a chance to handle the exception. They are shown in the Except block.

The program continues execution after exception handling.

The example program is given in the below code.

a=int(input("enter first number"))
b=int(input("enter second number"))
try:
    c=a/b
except:
    b+=1
    c=a/b
    print(c)
print("after exception")# continuation of code after exception handling

Output:
enter first number12
enter second number0
12.0
after exception

In the above program, after handling the exception, the python interpreter continues executing the remaining program.

So The print statement after an exception is displayed in the program.

Multiple Exceptions

a=int(input("enter first number"))
b=int(input("enter second number"))
z=int(input("enter list element index"))
lis=[1,2,3]
try:
    c=a/b
    print(lis[z])
except ZeroDivisionError:
    b+=1
    c=a/b
    print(c)
except IndexError:
    print("index out of bound")
print("after exception")# continuation of code after exception handling

Output:
enter first number12
enter second number0
enter list element index2
12.0
after exception

In the program above, we have a chance of obtaining two exceptions.

First one divide by zero exception.

List index out of bound is our second exception.

We have to handle the two exceptions separately.

To catch the exception separately, we need two except blocks.

The above program catches exceptions separately by using the name of the exception.

Python gave separate names to every exception. We had seen the names in our last class.

One more example is given below. Analyze the output for better practice.

a=int(input("enter first number"))
b=int(input("enter second number"))
z=int(input("enter list element index"))
lis=[1,2,3]
try:
    c=a/b
    print(lis[z])
except ZeroDivisionError:
    b+=1
    c=a/b
    print(c)
except IndexError:
    print("index out of bound")
print("after exception")# continuation of code after exception handling

Output:
enter first number12
enter second number2
enter list element index3
index out of bound
after exception

Exception Handling Exception class

In python, they have written a base class named exception to handle the exceptions.

a=int(input("enter first number"))
b=int(input("enter second number"))
z=int(input("enter list element index"))
lis=[1,2,3]
try:
    c=a/b
    print(lis[z])
except Exception as z:
    print(z)
    b+=1
    c=a/b
    print(c)
print("after exception")

Output:
enter first number12
enter second number2
enter list element index3
list index out of range
4.0
after exception

In the above program, we have a chance of two exceptions.

One except block is written with the name Exception.

Exception as z will take the default messages given by the python. The default message is shown in the previous class.

Print(z) will display the default message.

The default message division by zero is displayed in the output.

Exception Handling Else Block

Whenever we do not have any exception, and we need to display a message in this situation.

We use else block. The else block code will be executed if no exception occurred in the try block.

Analyze the output of the program given below for better practice.

a=int(input("enter first number"))
b=int(input("enter second number"))
z=int(input("enter list element index"))
lis=[1,2,3]
try:
    c=a/b
    print(lis[z])
except Exception as z:
    print(z)
    b+=1
    c=a/b
    print(c)
else:
    print("there is no exception")
print("after exception")

Output:
enter first number12
enter second number2
enter list element index2
3
there is no exception
after exception

Exception Handling Finally Block

Finally, the block is executed all the time.

If an exception is raised or exception is not raised. Both the situations finally will execute.

When is the finally block used?

Example:

When we are using files, we open a file and write some code regarding file operations.

Suppose there is an exception during file operations. We move to except block. After handling the exception, we need to close the file.

Closing file code will be written in the finally block.

The finally block will be executed every time.

The example programs are given below.

a=int(input("enter first number"))
b=int(input("enter second number"))
z=int(input("enter list element index"))
lis=[1,2,3]
try:
    c=a/b
    print(lis[z])
except Exception as z:
    print(z)
    b+=1
    c=a/b
    print(c)
else:
    print("there is no exception")
finally:
    print("this excexutes always")
print("after exception")

Output:
enter first number12
enter second number2
enter list element index2
3
there is no exception
this excexutes always
after exception

Stop Execution After Exception Handling

If we want to stop the execution of the program after exception handling. We use the raise keyword.

Raise keyword will raise an exception.

When to use the keyword raise?

We understand in the next user-defined exception class.

In the above program, we have two chances of exception.

One exception is divide by zero. And the other exception is index error.

If the divide by zero exception occurred after handling an exception, the raise keyword would divide by zero exception.

If an index error exception is occurred after handling an exception, the raise keyword will raise the index error exception.

Analyze the output of the program given below for better practice.

a=int(input("enter first number"))
b=int(input("enter second number"))
z=int(input("enter list element index"))
lis=[1,2,3]
try:
    c=a/b
    print(lis[z])
except Exception as z:
    b+=1
    c=a/b
    print(c)
    raise # used to raise exception to stop execution
else:
    print("there is no exception")
finally:
    print("this excexutes always")
print("after exception")

Output:
enter first number12
enter second number0
enter list element index2
12.0
this excexutes always
ZeroDivideError