-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest_Exception.java
More file actions
189 lines (166 loc) · 3.8 KB
/
Test_Exception.java
File metadata and controls
189 lines (166 loc) · 3.8 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
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
package com.May2023;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test_Exception extends P
{
//TEST the PROPAGATION of an Exception through method calls
public static void method1()
{
method2();
System.out.println("In method 1");
}
public static void method2()
{
try {
String str = null;
str.length();
}
catch(Exception e)
{
System.out.println("print exception");
}
System.out.println("In method 2");
}
public static void return_tryCatch()
{
try
{
System.out.println(3/0);
return;
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
//finally will run even if there is return keyword in the try block
System.out.println("in finally");
}
}
public static void static_exception_in_finally()
{
//If both try and finally block throw exception then the finally block exception will consume the try block exception
try {
System.out.println("try");
throw new ArrayIndexOutOfBoundsException();
}
catch(ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
}
finally
{
throw new ArithmeticException();
}
}
//Try method throws EXCEPTION and finally returns a value
//****Exception is thrown in try block which gets handled in Catch . PrintStackTrace is printed and return 1 in finally consumes the exception from try and only 1 is printed***
public int return_finally1()
{
//exception is thrown here
try
{
System.out.println(4/0);
}
//Exception is handled in catch
catch(ArithmeticException e)
{
System.out.println("exception handled- "+e); // this gets printed
e.printStackTrace(); // this gets printed
}
finally
{
return 1; //output is 1
}
}
//Since exception does not get handled in Catch , finally return value gets printed in the console
public int return_finally2()
{
//exception is thrown here
try
{
System.out.println(4/0);
}
//Exception is NOT handled in catch
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("exception handled- "+e);
e.printStackTrace();
}
finally
{
return 1; //output is 1
}
}
//Return keyword in both try and finally
//***return value in finally consumes the try block code***
public int return_finally_try()
{
try
{
return 5;
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
return 1; //output is 1
}
}
//sub class can declare without exception
public void test_Exception() throws FileNotFoundException
{
System.out.println("In Parent class");
}
public void test_Exception2() throws ArithmeticException
{
System.out.println("exception 2 in child class");
}
public int test_returnInTry()
{
try {
return 1 ;
}
catch(Exception e)
{
return 2;
}
finally
{
System.out.println("finally block");
}
}
public static void main(String[] args)
{
//method1();
//System.out.println("In main method");
//return_tryCatch();
//static_exception_in_finally();
Test_Exception ex = new Test_Exception();
//ex.test_Exception();
//ex.test_Exception2();
//System.out.println(ex.return_finally_try());
//ex.return_finally_try();
//System.out.println(ex.test_returnInTry());
System.out.println(ex.return_finally());
//Test_Exception.static_exception_in_finally();
}
}
//******TO TEST - METHOD OVERRIDING WITH EXCEPTION*******
class P
{
//parent class throws CHECKED EXCEPTION
public void test_Exception() throws IOException
{
System.out.println("In Parent class");
}
//parent class DOES NOT throw any exception
public void test_Exception2()
{
System.out.println("exception 2");
}
//parent class throws
}