Technology
CORE JAVA   CORE JAVA
JSP   JSP
JDBC   JDBC
RMI   RMI
HIBERNATE   HIBERNATE
OOP   OOP
JAVA   JAVA
 
 



ok  Can we throw exception from finally block in JAVA ? ? ?


Share Question and Answer -

Yes we can do... I am giving you one example try to understand it, if you having any doubt or quarry than feel free to post at http://www.java2all.com/5/QA

EX :

 class Throw_Demo
{
	public static void a()
	{
		try
		{
			System.out.println("Before b");
			b();
			System.out.println("********");
			System.out.println("");
			System.out.println("After b");
		}
		catch(ArithmeticException e)
		{
			System.out.println("a : " + e);
		}
	}
	
	public static void b()
	{
		try
		{
			System.out.println("Before c");
			c();
			System.out.println("********");
			System.out.println("");
			System.out.println("After c");
		}
		catch(ArithmeticException e)
		{
			System.out.println("b : " + e);
		}
		catch(ArrayIndexOutOfBoundsException j)
		{
			System.out.println("J : " + j)	;
		}
	}
	
	public static void c()
	{
		try
		{
			System.out.println("Before d");
			d();
			System.out.println("********");
			System.out.println("");
			System.out.println("After d");
		}
		catch(ArithmeticException e)
		{
			System.out.println("c : " + e);
			//throw new ArrayIndexOutOfBoundsException("demo");
		}
	}
	
//	@SuppressWarnings("finally")
	public static void d()
	{
		try
		{
			int i=1;
			int j=0;
			System.out.println("Before Division");		
			System.out.println(i/j);
			System.out.println("After Division");		
		}
		catch(ArithmeticException e)
		{
			System.out.println("d : " + e);
			
		}
		finally
		{
			throw new ArrayIndexOutOfBoundsException("demo");
		}
	}
	
	public static void main(String args[])
	{
	
		try
		{
			System.out.println("Before a");
			a();
			System.out.println("********");
			System.out.println("");
			System.out.println("After a");
		}
		catch(ArithmeticException e)
		{
			System.out.println("Main Program : " + e);
		}
	}
}

 
Output :
 
Before a
Before b
Before c
Before d
Before Division
d : java.lang.ArithmeticException: / by zero
J : java.lang.ArrayIndexOutOfBoundsException: demo
********
 
After b
********
 
After a