Exception Propagation/Exception Chaining
Unchecked exceptions are automatically forwarded in a calling chain. And this concept is also known as exceptions propagation.
In Checked exceptions it is mandatory to put handler where exception arises.
For Example:
class Temp
{
//Exception will occur in this method
void show()
{
int x = 10/0;
}
void display()
{
show();
}
void xyz()
{
display();
}
public static void main(String[] s)
{
Temp t = new Temp();
try
{
t.xyz();
}
//we will catch the Exception here
catch(ArithmeticException e)
{
System.out.println(e);
System.out.println();
e.printStackTrace();
}}}
Output:
In the above example we can see that arithmetic exception arises in show() function and it is caught in the main. So it is automatically forwarded in the calling chain.
*** e.printStackTrace(); —> this statement will print the tracing tree steps.
No comments:
Post a Comment