Advertisement



< Prev
Next >



Python - Exception Propagation





In this article, we are going to discuss a very important topic - exception propagation. In order to understand the concept of exception propagation i.e. what happens when an exception occurs in our program and how it travels through our program after it is originated? For the answer, let us continue reading to know about how exactly an exception propagates through our program after it is raised in it.




What happens when an exception is thrown by the program?


Let's consider a scenario, in which your module starts its execution and calls a function named func1(), which in turn calls another function func2(), which in turn calls another function func3(), now the call stack should look like the figure below-:



Looking at the diagram above, the currently executing function is at the top of the call stack, which in this example is func3(). Let's say while executing func3(), an exception is raised/thrown. Now this exception is thrown down to the next function in the call stack, i.e. func2(), which in turn throws the exception down to next method in the call stack, i.e. funct1(). The process continues until the exception is thrown at the bottom of the call stack i.e. to the statement in the module which called func1().

Now, at this time call stack explodes which leads to the abrupt termination of the program. Let's see how this actually takes place through a program in execution.




Advertisement




Exception propagation example


Let's see an example which explains how exactly exception propagates in a Python program. In this upcoming program, we are going to create a class named Exp with its three functions, named : In this program, a statement calls func1(), which calls function func2(), which in turn calls function func3(). Let's see the program.

#Defining a class named Exp and its three functions
class Exp :

    #First function of class Exp
    def func1(self):
		#Using self keyword to call a function from the inside of another function
        self.func2() 

        

    #Second function of class Exp
    def func2(self):
        self.func3()
        


    #Third function of class Exp
    def func3(self):
        print(100/0)	
	print("Hello from funct3()")
        


		
#Creating an object of class Exp
ob = Exp()


#Calling the function method1() of class Exp
ob.func1()


#This statement won't be executed because of an exception rising before the execution of this statement
print("Hello")


Output -:


Traceback (most recent call last):
  File "D:/Python Programs/excp3.py", line 28, in <module>
    ob.method1()
  File "D:/Python Programs/excp3.py", line 6, in func1
    self.method2()
  File "D:/Python Programs/excp3.py", line 12, in func2
    self.method3()
  File "D:/Python Programs/excp3.py", line 18, in func3
    print(100/0)
ZeroDivisionError: division by zero


In the code above, when the func3() is executed, an exception of type ZeroDivisionError is thrown at the runtime by the interpreter, because an integer number(100) is divided by a zero in it, without executing the print statement that had to print a Hello from func3() message. .

Consequently, function func3() throws this exception down to the next function in the call stack, i.e. func2(), which throws this exception down to the next function in the call stack, i.e. func1() and eventually this exception is thrown to the statement in module which called the function func1() , i.e.ob.func1()

At this time, the program terminates abruptly, without executing the print statement that had to print a Hello message. And as you can see in the output of the program, the program prints a trackback to the exception, describing the exception and where it was originated.

Hence, it is important to handle the exception in a program, so that an exception doesn't alter the normal flow of the program and ends it unexpectedly.

The process of handling an exception in a program is called Exception Handling. We will learn about the strategy of Exception Handling in the next article.



Please share this article -





< Prev
Next >
< Python Exception Handling
Python try, except block >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement