-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter_14_ps.java
More file actions
90 lines (81 loc) · 2.42 KB
/
Chapter_14_ps.java
File metadata and controls
90 lines (81 loc) · 2.42 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
//* Practice Set of the Exception Handling
import java.util.Scanner;
// ? Make Custom Exception
// Question-4
class ExceptionRetry extends Exception {
/**
* @return String
*/
@Override
public String getMessage() {
// TODO Auto-generated method stub
return "Error!";
}
}
public class Chapter_14_ps {
// Question-5
public static void throwException(int i) throws ExceptionRetry {
if (i <= 5) {
throw new ExceptionRetry();
}
}
public static void main(String[] args) {
// Write a java program to demonstrate syntax, logical 2 runtime errors.
// Question-1 Syntax Error
// int num1 = 10 // This is syntax error i am add the semicolon at the end of
// the line
// Question-1.2 Logical Error
int num1 = 10;
if (num1 == 11) {
System.out.println("I am 11");
} else {
System.out.println("I an not 11");
}
// * Output: I am not 11
// Question-2 Write a java program that prints "HaHa" during Arithmetic
// exception and "HeHe" during an Illegal argument exception.
try {
int a = 5;
int b = 0;
int c = a / b;
System.out.println(c);
} catch (IllegalArgumentException e) {
// TODO: handle exception
System.out.println("Hehe");
} catch (ArithmeticException a) {
System.out.println("Haha");
}
// Question-3
boolean flag = true;
int[] marks = new int[5];
marks[0] = 10;
marks[1] = 20;
marks[2] = 30;
marks[3] = 40;
marks[4] = 50;
// Create instance of the Scanner class
Scanner sc = new Scanner(System.in);
int index;
int i = 0;
while (flag || i < 5) {
try {
index = sc.nextInt();
System.out.printf("The value of marks[%d] is : %d", index, marks[index]);
break;
} catch (Exception e) {
// TODO: handle exception
System.out.println("Invalid Index!");
i++;
}
}
if (i >= 5) {
try {
throw new ExceptionRetry();
} catch (Exception e) {
// TODO: handle exception
System.out.println("Error");
}
}
sc.close();
}
}