Saturday, 25 December 2021

Finally block in Java

finally statement is used to handle an exception that is caught by any of the catch statement. Finally block is a default exception handler, it is always executed regardless of  whether the exception is raised and handled or not.

class JavaErrorDemo3 
{
	public static void main(String[] args) 
	{
		int a[]={5,10},b=5;
		int x,y;
		try
		{
		  x=a[2]/(b-a[1]);	
		}
		catch(ArithmeticException ex)
		{
			System.out.println("Division by zero");
		}
		catch(ArrayIndexOutOfBoundsException ex)
		{
			System.out.println("Array Index error");
		}
		catch(ArrayStoreException ex)
		{
			System.out.println("Wrong data type");
		}
		finally
		{
		y=a[1]/a[0];
		System.out.println("Value of Y is="+ y);
		}
	}
}

Output

D:\java examples>javac JavaErrorDemo3.java
D:\java examples>java JavaErrorDemo3
Array Index error
Value of Y is=2

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.

Find Us On Facebook

Computer Basics

More

C Programming

More

Java Tutorial

More

Data Structures

More

MS Office

More

Database Management

More
Top