-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionHandling.java
More file actions
58 lines (58 loc) · 2.18 KB
/
Copy pathExceptionHandling.java
File metadata and controls
58 lines (58 loc) · 2.18 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
48
49
50
51
52
53
54
55
56
57
58
package javalab;
import java.util.Scanner;
public class ExceptionHandling
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
char confirmation;
do
{
System.out.println("Enter your choice");
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 the number to be divided");
int divident=sc.nextInt();
System.out.println("Enter the divisor");
int divisior=sc.nextInt();
int result=divident/divisior;
System.out.println("The result is "+result);
}
catch(ArithmeticException e)
{
System.out.println("Caught an exception "+e.getMessage());
}
break;
case 2:
try
{
int array[]= {2,5,8,9};
System.out.println("Enter the index you want to acess ");
int index=sc.nextInt();
System.out.println("The number in "+index +" is "+array[index]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Caught an exception "+e.getMessage());
}
break;
default:
System.out.println("Invalid choice");
break;
}
System.out.println("DO YOU WANT TO CONTINUE : type Y or N");
confirmation=sc.next().charAt(0);
}
while(confirmation=='y' || confirmation=='Y');
if(confirmation=='n' || confirmation=='N')
{
System.out.println("Exited from code");
}
}
}