-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathThrowsEx1.java
More file actions
37 lines (32 loc) · 1.17 KB
/
ThrowsEx1.java
File metadata and controls
37 lines (32 loc) · 1.17 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 throws keyword.
Throws keyword is used to indicate that a method can throw an exception.
- Added by Kushagra Jaiswal (github.com/KUSHAGRA-JAISWAL)
*/
package src.college.understanding_exceptions;
import java.io.*;
class Cal {
int n1, n2;
void accept() throws ArithmeticException, NumberFormatException {
Console c = System.console();
n1 = Integer.parseInt(c.readLine("Enter num 1:"));
n2 = Integer.parseInt(c.readLine("Enter num 2:"));
}
void display() throws ArithmeticException, NumberFormatException {
System.out.println("The addition is " + (n1 + n2));
System.out.println("The subtraction is " + (n1 - n2));
System.out.println("The division is " + (n1 / n2));
System.out.println("The multiplication is " + (n1 * n2));
}
public static void main(String[] args) {
try {
Cal o = new Cal();
o.accept();
o.display();
} catch (ArithmeticException e) {
System.out.println("Arthmetic Error");
} catch (NumberFormatException e) {
System.out.println("Numberformat Error");
}
}
}