exp 3b
exp 3b
exp 3b
70
Ex. No:3b
PROGRAMS ON EXCEPTION HANDLING
Date:09.10.2024
Aim:
i)To write a program to handle exception using try catch and use “finally” block
to handle exception.
ii) To write a program to handle exception using “throw” keyword
iii) To write a program to handle exception using “throws” keyword.
71
72
for(int j=0;j<2;j++) {
arr[i][j]/=d;
}
}
System.out.println("Modified matrix:");
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
System.out.print(arr[i][j]+" ");
}
System.out.print("\n");
}
}
catch(ArithmeticException e) {
System.out.println("Error:Division by zero occurs");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Error:Array element doesn't exist");
}
finally {
System.out.print("Program ended \n");
}
}
}
73
OUTPUT:
74
(ii) Matrix operation with throw keyword
Algorithm:
a) Start
(b) Create a class “finall” and create a 2 dimensional array and get input.
(c) Use if else to check a random condition and give a new error using throw
keyword.
(d) If no error,isplay that modified matrix. If not,throw the error message.
(e) Stop.
PROGRAM:
package ex3b;
import java.util.Scanner;
public class finall {
public static void main(String[] args ) {
Scanner ip=new Scanner(System.in);
int arr[][]=new int[2][2];
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
System.out.println("Enter element ["+i+"]["+j+"] -");
arr[i][j]=ip.nextInt();
}
}
System.out.println("Enter the index of desired element to
divide all elements in array: ");
int x=ip.nextInt();
int y=ip.nextInt();
if(x<0 || y<0)
throw new ArithmeticException("Negative index not
allowed");
if(x>=2 || y>=2)
throw new ArrayIndexOutOfBoundsException("Doesn't exist
");
int d=arr[x][y];
if(d==1)
throw new ArithmeticException("Dividing by 1 returns same
");
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
arr[i][j]/=d;
}
}
System.out.println("Modified matrix:");
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
75
76
System.out.print(arr[i][j]+" ");
}
System.out.print("\n");
}
}
}
77
OUTPUT:
78
(iii) Matrix operation with throws keyword
Algorithm:
(a) Start
(b) Create a class “finall” and create a 2 dimensional array and get input.
(c) Mention the possible exception name with class name itself with throws
keyword.
(d) If no error, display that modified matrix. If not,it throws the error message.
(e) Stop.
PROGRAM:
package ex3b;
import java.util.Scanner;
public class finall {
public static void main(String[] args ) throws
ArithmeticException,ArrayIndexOutOfBoundsException{
Scanner ip=new Scanner(System.in);
int arr[][]=new int[2][2];
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
System.out.println("Enter element ["+i+"]["+j+"] -");
arr[i][j]=ip.nextInt();
}
}
System.out.println("Enter the index of desired element to
divide all elements in array: ");
int x=ip.nextInt();
int y=ip.nextInt();
int d=arr[x][y];
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
arr[i][j]/=d;
}
}
System.out.println("Modified matrix:");
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
System.out.print(arr[i][j]+" ");
}
System.out.print("\n");
}
}
}
79
Criteria Marks
Program 1 and its Output Verification / 20
Program 2 and its Output Verification / 20
Program 3 and its Output Verification / 20
Record Preparation / 30
Viva /10
Total /100
Result:
Thus the program
i) To handle exception using try catch and use “finally” block to handle
exception.
ii) To handle exception using “throw” keyword.
iii) To handle exception using “throws” keyword.
Are performed and executed and inferred successfully.
80