wellnwill Marathahalli Bangalore

Nesting of try catch finally


The exception which is generated in nested try block can also be handled in it’s outer catch block but reverse is not possible i.e. if there is no exception handler for a particular exception generated in inner try block will be propagated to outer catch.

class Test229
{
public static void main(String[] args)
{
System.out.println("main() start");
try
{
 try
 {
 System.out.println("1st inner try block");
 int a=5/0;
 System.out.println(a);
 }
 catch (ArithmeticException ae)
 {
 System.out.println("1st inner catch block"+ae);
 }
 try
 {
 System.out.println("2nd inner try block");
 int[] a={1,2,3};
 System.out.println(a[4]);
 }
 catch (ArrayIndexOutOfBoundsException aie)
 {
 System.out.println("2nd inner catch block"+aie);
 }
}
catch (Exception e)
{
System.out.println("outer catch"+e);
}
}
}
//O/P-main() start,1st inner try block, 1st inner catch with exception message,2nd inner try block,2nd inner catch block
with exception message.

Exception Propagation

At runtime if exception occurs in top of the stack and if there is no exception handler in that method then that exception will pass on the next caller method in stack by removing current method forcefully.

Again in that caller method if there is no exception handler, exception will be passed on to next caller method in stack by removing current method forcefully. This process will be repeated until an exception is not caught or exception reaches bottom of the stack.

If exception reaches bottom of the stack then JVM will use default handler and print the message to the console and terminate the program abnormally.

Note- by default Unchecked Exceptions are propagated in calling chain. by default checked Exceptions are not propagated in calling chain


throws keyword

Advantage of throws keyword: Checked Exception can be propagated.

Throws is a keyword that can be used to specify the information about an exception that a method may throw.

In java, an exception can be handled in the following two ways:

  • By using try-catch blocks
  • By using throws keyword

Checked Exception will be propagated to calling chain by using throws keyword

import java.io.IOException;
class Test230
{
	void m1() throws IOException  // checked exception will not be propagated by default. Use throws keyword.
	{
		IOException  e=new IOException("checked exception");
		throw e;
	}
public static void main(String[] args) //caller mehod
{
System.out.println("main starts");
Test230 t=new Test230();
try
{
 t.m1();
}
catch(IOException e)
{
	System.out.println(e);
}
System.out.println("main ends");
}
}
class Test226
{
	void m1() //throws ArithmeticException// unchecked exception will be propagated by default
	{
		ArithmeticException  e=new ArithmeticException("/ by zero");
		throw e;
	}
public static void main(String[] args) //caller mehod
{
System.out.println("main starts");
Test226 t=new Test226();
try
{
 t.m1();
}
catch(ArithmeticException  e)
{
	System.out.println(e);
}
System.out.println("main ends");
}
}

Footer with Map

Top