-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisplayException.java
110 lines (93 loc) · 2.97 KB
/
DisplayException.java
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.util.Scanner;
class ExceptionHandle {
void Arithmetic() {
// Method to Handle Arithmetic Exception
Scanner sc = new Scanner(System.in);
// Two integers are taken as input
System.out.println("Enter two numbers: ");
System.out.print("First Number: ");
int num1 = sc.nextInt();
System.out.print("Second Number: ");
int num2 = sc.nextInt();
// If the second number is zero then Exception is handled otherwise quotient is
// displayed
try {
int data = num1 / num2;
System.out.print("The quotient is " + data);
} catch (ArithmeticException e) {
System.out.println(e);
System.out.println("Dividing by zero is Invalid!!");
}
}
void NumberFormat() {
// Method to handle Number format Exception
Scanner sc = new Scanner(System.in);
System.out.println("Enter two numbers: ");
// Two string type inputs are taken from user
System.out.print("First Number: ");
String x = sc.next();
/* If the string consists of only digits it can be converted to integer and
displayed otherwise exception is handled*/
try {
System.out.println("the first number is " + Integer.parseInt(x));
} catch (NumberFormatException ex) {
System.err.println(ex);
System.out.println("Wrong input format. Please enter an integer");
}
System.out.print("Second Number: ");
String y = sc.next();
try {
System.out.println("the second number is " + Integer.parseInt(y));
} catch (NumberFormatException ex1) {
System.err.println(ex1);
System.out.println("Wrong input format. Please enter an integer");
}
}
void ArrayIndex() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of array: ");
// reading the number of elements from the that we want to enter
int n = sc.nextInt();
int[] array = new int[n];
System.out.print("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
// reading array elements from the user
array[i] = sc.nextInt();
}
System.out.print("Enter index of element you want to access: ");
int index = sc.nextInt();
try {
System.out.println("Element pesent at "+index+" is "+array[index]);
} catch (ArrayIndexOutOfBoundsException a) {
System.err.println(a + "\nDesired Index cannot be accessed");
}
}
}
public class DisplayException {
public static void main(String[] args) {
// Menu driven code to handle the desired exception
while (true) {
System.out.print("\n\t\tMenu\n\t1. Handle Arithmetic Exception\n\t2. Handle Number Format Exception\n\t3. Handle Array Index Out of Bound Exception \n\t4. Exit");
Scanner s = new Scanner(System.in);
int ch = s.nextInt();
ExceptionHandle obj = new ExceptionHandle(); //object creation
switch (ch) {
case 1:
obj.Arithmetic(); //Method calling
break;
case 2:
obj.NumberFormat(); //Method calling
break;
case 3:
obj.ArrayIndex(); //Method calling
break;
case 4:
System.exit(0); //Exit
break;
default:
System.out.println("Invalid Choice");
break;
}
}
}
}