You are on page 1of 2

Ex No.

20
Date:
Exception Handling
Aim:
To write a java program to illustrate exception handling.
Source Code:
import java.io.*;
public class exceptionHandle{
public static void main(String[] args) throws Exception{
try{
int a, b, c;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
a = Integer.parseInt(in.readLine());
b = Integer.parseInt(in.readLine());
c=a/b;
System.out.println("C = " + c);
}
catch (NumberFormatException ex){
System.out.println(ex.getMessage() + " is not a numeric value.");
System.exit(0);
}
catch(ArithmeticException ae){
System.out.println(ae.getMessage() + " is Zero.");
System.exit(0);
}
}
}
Output:
Javac exceptionHandle.java
java exceptionHandle
2
0
/ by zero is Zero.
java exceptionHandle
6
2
C = 3
Result:
Thus a java program has been written for catching Exception.