< Prev
Next >



Exception Propagation





Do you know what happens when an exception occurs in our program and how it travels through our program after it is originated? Let's continue reading to know about how exactly an exception propagates through our code after it is raised.




What happens when an exception is thrown by the program?


Considering a scenario, in which your program starts its execution at the main() function, main() calls function1(), function1() calls function2(), function2() calls function3(), now the call stack should look like the figure below-:


The currently executing function is at the top of the call stack, which in this example is function3(). Let's say while executing function3(), an exception is raised/thrown. Now this exception is thrown down to the next function in the call stack, i.e. function2(), which in turn throws the exception down to next function in the call stack, i.e. function1(). The process continues until the exception is thrown to the function at the bottom of the call stack, i.e. main() function.
Now, at this time call stack explodes which leads to the abrupt termination of the program.






Exception propagation example


#include<iostream>

using namespace std;

class Excp
{
public: 
void function1();
void function2();
void function3();
};



void Excp :: function1()
{	
	function2();
};



void Excp :: function2()
{
	function3();
}


void Excp :: function3()
{
	int x = 20;
	int y = 20;
	int result = x-y;
	if(result==0)
	  	throw(result);
	else
	cout<<(x/result);

	cout<<"Hello"; //This statement will not be executed.
}




int main()
{
Excp ob;
try
{
	ob.function1();
}
catch(int e)
{
	cout<<"Caught exception : int type \n";	
}
}


Output -:


Caught exception : int type


In the code above, the main() functions calls the function1() function, which calls function2() function, which inturn calls the function3() function. Within the function3() function, compiler finds a division-by-zero problem, hence an exception of type int is thrown when a statement enclosed within the try block is executed.

The function3() throws this exception down to the next function in the call stack, i.e. function2(), which throws the exception down the next function in the call stack, i.e. function1() and the process continues until this exception reaches the function at the bottom of the call stack, i.e. main() function. At this time, the exception is caught by the matching catch block and the program ends smoothly.

Note : Although the program doesn't end abruptly but it completes its execution without executing the cout statement that had to print a Hello message,

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 strategy of Exception Handling in the next article.



Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< What is Exception?
Try-Catch block >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
Python

Ad2