-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path27_try-catch statements
206 lines (176 loc) · 4.99 KB
/
27_try-catch statements
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package com.company;
public class try_catch {
public static void main(String[] args) {
int a = 3000;
int b = 0;
try{
int c = a/b;
System.out.println("The result is:" +c);
}
catch (Exception e){
System.out.println("We failed to divide. Reason: ");
System.out.println(e);
System.out.println("End of the program");
}
}
}
// Specific exceptions
package com.company;
import java.util.Scanner;
public class Specific_exceptions {
public static void main(String[] args) {
int[] marks = new int[3];
marks[0]=30;
marks[1]=38;
marks[2]=49;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array index: ");
int ind = sc.nextInt();
System.out.println("Enter the number you want to dic=vide with: ");
int number = sc.nextInt();
try{
System.out.println("The value at array index entered is: " +marks[ind]);
System.out.println("The value of array-value/number is: "+marks[ind]/number);
}
catch (ArithmeticException e){
System.out.println("Some exception occured!");
System.out.println(e);
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println("Some exception occured!");
System.out.println(e);
}
catch (Exception e){
System.out.println("Some other exception occured!");
System.out.println(e);
}
}
}
//Nested try-catch
package com.company;
import java.util.Scanner;
public class Nested_try_catch {
public static void main(String[] args) {
int[] marks = new int[3];
marks[0]=30;
marks[1]=38;
marks[2]=49;
Scanner sc = new Scanner(System.in);
boolean flag = true;
while (flag) {
System.out.println("Enter the array index: ");
int ind = sc.nextInt();
try {
System.out.println("Welcome to this world");
try {
System.out.println(marks[ind]);
flag = false;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Sorry this index does not exist!");
System.out.println("Exception in level 2");
// flag = false;
}
} catch (Exception e) {
System.out.println("Exception in level 1");
}
}
}
}
//Exception class
package com.company;
import java.util.Scanner;
class MyException extends Exception{
@Override
public String toString(){
return super.toString();
}
@Override
public String getMessage() {
return super.getMessage();
}
}
public class Exception_class {
public static void main(String[] args) {
int a;
Scanner sc = new Scanner(System.in);
a=sc.nextInt();
if (a<9){
try {
throw new MyException();
}
catch(Exception e){
System.out.println(e.getMessage());
System.out.println(e.toString());
e.printStackTrace();
}
}
}
}
//throw-throws kwywords
package com.company;
class NegativeRadiusException extends Exception{
@Override
public String toString() {
return "Radius cannot be negative";
}
@Override
public String getMessage() {
return "Radius cannot be negative";
}
}
public class Throw_throws {
public static double area(int r) throws NegativeRadiusException{
if (r<0){
throw new NegativeRadiusException();
}
double result = Math.PI*r*r;
return result;
}
public static int divide(int a, int b) throws ArithmeticException{
int result = a/b;
return result;
}
public static void main(String[] args) {
try {
// int c = divide(3,3);
// System.out.println(c);
double ar = area(0);
System.out.println(ar);
}
catch (Exception e){
System.out.println("Exception");
}
// double ar = area(5);
}
}
// finally keyword
package com.company;
public class Finally {
public static int greet() {
try {
int a = 60;
int b = 4;
int c = a / b;
return c;
} catch (Exception e) {
System.out.println(e);
} finally {
System.out.println("This is the end of this function");
}
return 0;
}
public static void main(String[] args) {
// try {
// int a = 5;
// int b = 0;
// int c = a/b;
// }
// catch (Exception e){
// System.out.println(e);
// }
// finally {
// System.out.println("This is the end of this program");
int k = greet();
System.out.println(k);
}
}