-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathExceptionDemo.java
More file actions
44 lines (39 loc) · 1.12 KB
/
ExceptionDemo.java
File metadata and controls
44 lines (39 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
38
39
40
41
42
43
44
import java.util.InputMismatchException;
import java.util.Scanner;
public class ExceptionDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
int firstNo = 0;
int secondNo = 0;
int result =0;
Scanner scanner =new Scanner(System.in);
System.out.println("Enter the First No");
try{
firstNo = scanner.nextInt(); // throw new java.util.InputMisMatchException()
}
catch(InputMismatchException e){
System.out.println("Only Number Allowed , U Enter Something else ");
System.out.println("Enter the First No Again");
scanner.nextLine();
firstNo = scanner.nextInt();
}
System.out.println("Enter the Second No");
try{
secondNo = scanner.nextInt();
}
catch(InputMismatchException e){
System.out.println("Only Number Allowed , U Enter Something else ");
System.out.println("Enter the Second No Again");
scanner.nextLine();
secondNo = scanner.nextInt();
}
try{
result = firstNo/secondNo;
}
catch(ArithmeticException e){
System.out.println("U Divide a Number with Zero "+e);
}
System.out.println("Result is "+result);
scanner.close();
}
}