< Prev
Next >

Exception Propagation

Do you know what happens when an exception suddenly occurs during the execution of our C# program and how this unhandled exception travels through our program after it is origination? Let's continue reading to know about what exactly happens when an exception happens in our program and how an unhandled 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() method, Main() calls Method1(), Method1() calls Method2(), Method2() calls Method3(), now the call stack should look like the figure below:

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


Exception propagation example

//C# Example of exception propagation

using System;

class Exp
//Defining a method named Method1() of class Exp
public void Method1()

//Defining a method named Method2() of class Exp
public void Method2()

//Defining a method named Method3() of class Exp
public void Method3()
	//Local variables
	int a = 100, b = 0;
	//DivideByZeroException is raised/thrown by the program.
	//This statement will not be executed.	

//Defining the Main() method of class Exp
public static void Main(String[] arr)
	//Creating an object of class Exp
	Exp ob = new Exp();

	//Calling the method Method1() of Exp class

Output -:

Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero.
   at Exp.Method3()
   at Exp.Method2()
   at Exp.Method1()
   at Exp.Main(String[] arr)

In the code above, within the current executing method i.e. Method3()an integer number(100) is divided by a zero(0), which leads to an exception of type DivideByZeroException thrown at the runtime by the common runtime library(CLR) managing the runtime environment.

This exception is thrown when the Method3() is being executed, Method3() throws this exception down to the next method in the call stack, i.e to the Method2() and the process continues until this exception reaches the method at the bottom of the call stack, i.e. the Main() method. At this time, program terminates abruptly, without executing the 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 -

< Prev
Next >
< C# Exception Handling
C# Try-Catch block >


Please Subscribe

Please subscribe to our social media channels for daily updates.

Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page



Please check our latest addition