-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment 6
More file actions
87 lines (80 loc) · 2.05 KB
/
Assignment 6
File metadata and controls
87 lines (80 loc) · 2.05 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
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
import java.util.*;
class Exception{
Scanner sc = new Scanner(System.in);
void Arthexc(){
try{
int n1, n2;
int rs = 0;
System.out.println("ENTER NUM 1: ");
n1 = sc.nextInt();
System.out.println("Enter num 2: ");
n2 = sc.nextInt();
rs = n1/n2;
}
catch (ArithmeticException e){
System.out.println(e);
}
System.out.println("REST OF THE CODE");
}
void ArrIndOutOfB(int[] arr){
System.out.println("Standard array is: ");
for(int i = 0; i<arr.length; i++){
System.out.print(arr[i]+" ");
}
System.out.println();
try{
System.out.println("Enter the index number of. array element which you want to print: ");
int n1 = sc.nextInt();
System.out.println(arr[n1]+"is present at given index");
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println(e);
}
System.out.println("Normal code flow...");
}
void NumForm(){
try {
System.out.println("Enter first number: ");
String s1 = sc.next();
System.out.println("Enter second number: ");
String s2 = sc.next();
int a = Integer.parseInt(s1);
int b = Integer.parseInt(s2);
System.out.println("Entered numbers are: " + a
+ " & " + b);
}catch(NumberFormatException e){
System.out.println(e);
}
System.out.println("Normal flow of code...");
}
}
public class Main1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int ch;
Exception e = new Exception();
do{
System.out.println("Which exception do you want to see?\n1.Arithmetic
Exception\t\n2.ArrayIndexOutOfBounds exception\t\n3.Number Format
Exception");
ch = sc.nextInt();
switch (ch){
case 1:
e.Arthexc();
System.out.println("==========================="); break;
case 2:
int[] array = {1, 2, 3, 4, 5};
e.ArrIndOutOfB(array);
System.out.println("===========================");
break;
case 3:
e.NumForm();
System.out.println("============================"); break;
default:
System.out.println("Enter valid input please!!!!");
break;
}
}
while(ch!=4);
}
}