Java- Example of finally

//Example of finally
//1. using throw
// 2. simple try-catch-finally
//3 Return stement in try block
class Ex_finally
{
/*
void call()//1. using throw
{
try{
System.out.println("Inside call()");
throw new RuntimeException("Demo");
}
finally{
System.out.println("finally called");
}
}
void call1()// 2. simple try-catch-finally
{
int a=5,b=1,c;
try{
System.out.println("Inside call1()");
c=a/b;
}
catch(Exception e)
{
System.out.println("don't devide number by zero");
}
finally{
System.out.println("finally of call1() called");
}
}
*/
void call2()//3 Return stement in try block
{
int a=5,b=1,c;
try
{
System.out.println("Inside call2()");
c=a/b;
return ;
}

catch(Exception e)
{
System.out.println("don't devide number by zero");
}
finally
{
System.out.println("finally of call2() called");
}
}
}
class Test
{
public static void main(String ar[])
{
Ex_finally obj=new Ex_finally();
try{
//obj.call();
//obj.call1();
obj.call2();
//System.out.println("result="+res);
}
catch(RuntimeException e)
{
System.out.println("call() Ecxception handled"+e);
}

}

}

Reactions: