-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathException.java
More file actions
47 lines (47 loc) · 1.3 KB
/
Copy pathException.java
File metadata and controls
47 lines (47 loc) · 1.3 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
45
46
47
package OOPJava;
import java.util.Scanner;
public class Exception {
public static void main(String[]args) {
Scanner sc=new Scanner(System.in);
char c=0;
do {
System.out.println("Choose an operation:");
System.out.println("1.Arithmetic Operation");
System.out.println("2.Array Access");
int choice=sc.nextInt();
switch(choice) {
case 1:
try {
System.out.println("Enter Numerator:");
int numerator=sc.nextInt();
System.out.println("Enter Denominator:");
int denominator=sc.nextInt();
int result=numerator/denominator;
System.out.println("Result="+result);
}
catch (ArithmeticException e) {
System.out.println("Caught an exception"+e.getMessage());
}
break;
case 2:
int [] numbers= {1,2,3};
try {
System.out.println("Enter array index to access:");
int index=sc.nextInt();
System.out.println("Element at index "+index +":" +numbers[index]);
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Caught an exception:"+e.getMessage());
}
break;
default:
System.out.println("Invalid Choice! Please enter 1 or 2");
}
System.out.println("Do you want to continue? Select y or n");
c=sc.next().charAt(0);
}
while (c=='y'|c=='Y');
System.out.println("Code successfully executed");
sc.close();
}
}