-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractice14_ErrorsException.java
More file actions
110 lines (95 loc) · 2.52 KB
/
Practice14_ErrorsException.java
File metadata and controls
110 lines (95 loc) · 2.52 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.company;
import java.util.Scanner;
class MaxExceptionError extends Exception{
@Override
public String toString() {
return "You've reached your limit";
}
@Override
public String getMessage() {
return "You've reached your limit";
}
}
public class Practice14_ErrorsException {
private static Scanner sc1;
public static int array(int i) throws MaxExceptionError{
if (i>2) {
throw new MaxExceptionError();
}
return i;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 1.
// Syntax Error
// int a = 7
// b = 4;
// Logical Error
// int yourAge = 2000;
// Runtime Error
// int a = 4;
// int b = 0;
// int c = a/b; // dividing by 0 is an error
// 2.
// System.out.println("Enter the value do you want to divide");
// int a = sc.nextInt();
// int b = sc.nextInt();
// int i = 0;
//
// try {
// int c = a / b;
// }
// catch (ArithmeticException e) {
// System.out.println("HEHE");
// }
// catch (IllegalArgumentException r) {
// System.out.println("HAHA");
// }
// 3.
// int [] z = new int[3];
// z[0] = 4;
// z[1] = 6;
// z[2] = 8;
// boolean flag = true;
// int l = 0;
// while (flag && l<5) {
// try {
// System.out.println("Enter the number");
// int k = sc.nextInt();
// System.out.println(z[k]);
// break;
// }
// catch (Exception ) {
// System.out.println("Invalid index please enter valid index");
// l++;
// }
// }
// System.out.println("Error");
// 4.
int [] z = new int[3];
z[0] = 4;
z[1] = 6;
z[2] = 8;
int k;
boolean flag = true;
int l = 0;
while (flag && l<5) {
try {
System.out.println("Enter the number");
k = sc.nextInt();
System.out.println(z[k]);
break;
}
catch (Exception e) {
System.out.println("Invalid index please enter valid index");
l++;
}
}
try {
throw new MaxExceptionError();
}
catch (Exception e) {
System.out.println(e);
}
}
}