Java2all.com  

Finally





Share This Topic -

Java supports another statement known as finally statement that can be used to handle an exception that is not caught by any of the previous catch statements.

We can put finally block after the try block or after the last catch block.

The finally block is executed in all circumstances. Even if a try block completes without problems, the finally block executes.

EX :

 public class Finally_Demo 
{
		public static void main(String args[])
		{
			int num1 = 100;
			int num2 = 50;
			int num3 = 50;
			int result1;
				
			try
			{
				result1 = num1/(num2-num3);
				System.out.println("Result1 = " + result1);
			}
			catch(ArithmeticException g)
			{
				System.out.println("Division by zero");
				
			}
			finally
			{
				System.out.println("This is final");
			}
			
		}
}

 
Output :
Division by zero
This is final