-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathThrowEx1.java
More file actions
37 lines (33 loc) · 1.12 KB
/
ThrowEx1.java
File metadata and controls
37 lines (33 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
Code to demonstrate the use of throw keyword.
- throw keyword is used to throw an exception forcefully/explicitly.
*/
package src.college.understanding_exceptions;
import java.io.*;
public class ThrowEx1 {
int a,b;
void accept() throws NumberFormatException {
Console c = System.console();
a = Integer.parseInt(c.readLine("Enter num1: "));
b = Integer.parseInt(c.readLine("Enter num2: "));
}
void display() throws ArithmeticException{
System.out.println("Sum is "+(a+b));
System.out.println("Difference is "+(a-b));
System.out.println("Product is "+(a*b));
System.out.println("Division is "+(a/b));
}
public static void main(String[] args) {
ThrowEx1 t = new ThrowEx1();
try {
t.accept();
if (t.b == 0)
throw new ArithmeticException("Division by zero");
t.display();
} catch (ArithmeticException e) {
System.out.println("Exception occured "+e);
} catch (NumberFormatException e) {
System.out.println("Exception occured "+e);
}
}
}