Aim:
To implement the pre defined exception concept in java
Algorithm:
- Start the program
- Create the called error.
- Declare and Initialize the data members.
- Use the try and catch block to handle the exception
- If the exception exists, the corresponding catch block will be executed or else control goes out of the catch block.
- 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…..