Java Program to Implement the Pre Defined Exception Concept

Aim:

To implement the pre defined exception concept in java

Algorithm:

  1. Start the program
  2. Create the called error.
  3. Declare and Initialize the data members.
  4. Use the try and catch block to handle the exception
  5. If the exception exists, the corresponding catch block will be executed or else control goes out of the catch block.
  6. Display the result.

Program:

import java.util.Scanner;
public class Main
{
public static void main (String [] args)
{
try
{
int []arr=new int[2]; 
System.out.println ("ENTER THE NUMBERS ");
Scanner put=new Scanner( System.in);
arr[0]=put.nextInt();
arr[1]=put.nextInt();
int n1=arr[0];
int n2=arr[1];
arr[2]=n1/n2;
System.out.println ("DIVISION VALUE = "+arr[2]);
}
catch (ArithmeticException Ae)
{
System.out.println ("DONT ENTER ZERO FOR DENOMINATOR...");
}
catch (NumberFormatException Nfe)
{
System.out.println ("PASS ONLY INTEGER VALUES...");
}
catch (ArrayIndexOutOfBoundsException Aioobe)
{
System.out.println ("ARRA OUT OF BOUND");
}
finally
{
System.out.println ("I AM FROM FINALLY...");
}
}
};

Execution:

INPUT : 
ENTER THE NUMBERS:  8  2

OUTPUT : 
ARRA OUT OF BOUND
I AM FROM FINALLY…..

Leave a Comment