-
Notifications
You must be signed in to change notification settings - Fork 0
/
exception-handling.html
372 lines (324 loc) · 15.2 KB
/
exception-handling.html
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<!--
~ Author: @tridib2003
~ Repository: https://github.com/tridib2003/levelupjava
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Exception Handling | Level-Up Java</title>
<link href="styles.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<nav class="navigation-std">
<ul class="ul-remove-bullet nav-pills">
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Previous</a>
</li>
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Home</a>
</li>
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Next</a>
</li>
</ul>
</nav>
<h1 class="h1-basic">Exception Handling</h1>
<div class="desc-container desc-container-center desc-std">
<hr>
<ul>
<li>
<p>
<i>Exception</i> is an undesired state that arises during run-time when instructions are executed.
Basically
a run-time error.
</p>
</li>
<li>
<p>
Five keywords used for exception handling in Java are: <strong>try</strong>, <strong>catch</strong>,
<strong>throw</strong>, <strong>throws</strong>, and <strong>finally</strong>.
<ul>
<li>
<p>
<strong>try</strong> - this block contains the set of statements that might cause
exceptions.
</p>
</li>
<li>
<p>
<strong>catch</strong> - this block will execute if an exception has occured in the
<i>try</i> block and perform necesssary steps, hence preventing abnormal termination of
the
program.
</p>
</li>
<li>
<p>
<strong>throw</strong> - to manually throw an exception the <i>throw</i> keyword is
used.
Although system-generated exceptions are automatically thrown by the Java run-time
system.
</p>
</li>
<li>
<p>
<strong>throws</strong> - any exception which is thrown out of a method must be
specified using the <i>throws</i> clause.
</p>
</li>
<li>
<p>
<strong>finally</strong> - set of statements which must be executed after <i>try</i>
block
completes must be put inside the <i>finally</i> block.
</p>
</li>
</ul>
</p>
</li>
<li>
<p>
The <strong>Throwable</strong> class is at the top of the exception class hierarchy. All exception
types are subclasses of the built-in class <i>Throwable</i>. There are two sub-classes of
<i>Throwable</i> named <strong>Error</strong> and <strong>Exception</strong>.
</p>
<p title="Throwable class hierarchy" style="text-align:center">
<img src="assets\images\LUJ-EH-01.png" alt="Throwable class hierarchy" width=500
class="img-responsive" />
</p>
</li>
<li>
<p>
The <strong>Error</strong> class defines exceptions that are not expected to be caught under normal
circumstances by our program. Exceptions of type <i>Error</i> are used by the Java run-time system
to indicate errors having to do with the run-time environment, itself. Ex.: Stack overflow error.
</p>
</li>
<li>
<p>
The <strong>Exception</strong> class is subclassed to created custom exception types to catch the
exceptional conditions encountered by the program.
</p>
</li>
<li>
<p>
The <strong>RunTimeException</strong> class is a subclass of <i>Exception</i>. The exceptions of
this type are automatically defined for the programs. Ex.: ArithmeticException,
IndexOutOfBoundsException, NullPointerException, etc.
</p>
</li>
<li>
<p>
Any exception that is not caught by your program will ultimately be processed by the default
handler. The default handler displays a string describing the exception, prints a stack trace from
the point at which exception has occured, and teminates the program.
</p>
</li>
<br>
<div class="syntax-container syntax-container-center syntax-std">
<p style="font-weight: bold;">
General syntax:
</p>
<xmp>
try {
// block of code to monitor for errors
}
catch (ExceptionType1 exOb){
// exception handler for Exception Type 1
}
catch (ExceptionType2 exOb){
// exception handler for Exception Type 2
}
// ...
finally {
// block of code to be executed after try block ends
}
</xmp>
</div>
<li>
<p>
As soon as an exception is thrown the program control transfers out of the <strong>try</strong>
block into the <strong>catch</strong> block. After the statements inside the <i>catch</i> block has
executed, program control continues with the next set of statements following the entire
<i>try-catch</i> module.
</p>
</li>
<li>
<p>
If a block of code can raise multiple types of exceptions, we can specify multiple <i>catch</i>
clauses to handle these different exceptional cases. As soon as an exception is thrown, each
<i>catch</i> clause is inspected in order and once a match is found, statements within that
<i>catch</i> block will be executed and others will be skipped and the control passes to the
statement immediately after the <i>try-catch</i> block. Also, any exception subclass must come
before its superclass, otherwise a subclass exception type would never be reached and resulting in
compile time error.
</p>
</li>
<li>
<p>
The <strong>throw</strong> statement is used to explicitly throw an exception. The keyword
<i>throw</i> is followed by an object of type <i>Throwable</i>, or a subclass of <i>Throwable</i>.
We can create a <i>Throwable</i> object either by using a parameter in a <i>catch</i> clause or via
creating one with the <i>new</i> operator. The execution stops immediately a <i>throw</i> statement
is encountered and statements following it are not executed. Then the nearest <i>try</i> block is
looked upon to find match for the exception type, if found then those statements are executed. If no
such matching <i>catch</i> block is found the default exception handler stops the program execution
and prints stack trace.
</p>
</li>
<li>
<p>
The <strong>throws</strong> clause is put after the parameter-list in the method's declaration, if a
method can cause exceptions that it does not take care of. After the <i>throws</i> clause the list
of all exceptions types that the method can throw are mentioned. If the <i>throws</i> clause is
missed out, it will result in a compile-time error. All types of exceptions, except those of type
<strong>Error</strong> or <strong>RuntimeException</strong>, or any of their subclasses must be
declared in the <i>throws</i> statement.
</p>
</li>
<li>
<p>
Statements within the <strong>finally</strong> will be executed whether or not an exception is
thrown. The <i>finally</i> block is executed just before the control returns to the caller from the
<i>try-catch</i> block. Although the <i>finally</i> clause if optional, each <i>try</i> statement
must have at least one <i>catch</i> or a <i>finally</i> clause.
</p>
</li>
</ul>
</div>
<br><br><br>
<div class="table-container table-container-center">
<table class="table-code-links">
<!-- <tr>
<th>Title</th>
<th>Desc</th>
</tr> -->
<tr>
<td>
Handling exceptions using try-catch mechanism
</td>
<td>
<div class="btn-container-center">
<a class="btn-link btn-link-color1"
href="https://github.com/tridib2003/levelupjava-wiki/blob/main/Exception%20Handling/HandlingException1.java"
target="_blank">
<i class="fa fa-github" style="font-size: 18px; color: whitesmoke;">
<p style="display: inline; margin-left: 0.4rem;">
View code
</p>
</i>
</a>
</div>
</td>
</tr>
<tr>
<td>
Handling exceptions and printing exception description
</td>
<td>
<div class="btn-container-center">
<a class="btn-link btn-link-color1"
href="https://github.com/tridib2003/levelupjava-wiki/blob/main/Exception%20Handling/HandlingException2.java"
target="_blank">
<i class="fa fa-github" style="font-size: 18px; color: whitesmoke;">
<p style="display: inline; margin-left: 0.4rem;">
View code
</p>
</i>
</a>
</div>
</td>
</tr>
<tr>
<td>
Handling exceptions using multiple catch statements
</td>
<td>
<div class="btn-container-center">
<a class="btn-link btn-link-color1"
href="https://github.com/tridib2003/levelupjava-wiki/blob/main/Exception%20Handling/MultipleCatches.java"
target="_blank">
<i class="fa fa-github" style="font-size: 18px; color: whitesmoke;">
<p style="display: inline; margin-left: 0.4rem;">
View code
</p>
</i>
</a>
</div>
</td>
</tr>
<tr>
<td>
Explicitly throw exceptions using the throw statement
</td>
<td>
<div class="btn-container-center">
<a class="btn-link btn-link-color1"
href="https://github.com/tridib2003/levelupjava-wiki/blob/main/Exception%20Handling/ThrowSample.java"
target="_blank">
<i class="fa fa-github" style="font-size: 18px; color: whitesmoke;">
<p style="display: inline; margin-left: 0.4rem;">
View code
</p>
</i>
</a>
</div>
</td>
</tr>
<tr>
<td>
Specify exceptions that might occur using the throws clause
</td>
<td>
<div class="btn-container-center">
<a class="btn-link btn-link-color1"
href="https://github.com/tridib2003/levelupjava-wiki/blob/main/Exception%20Handling/ThrowsSample.java"
target="_blank">
<i class="fa fa-github" style="font-size: 18px; color: whitesmoke;">
<p style="display: inline; margin-left: 0.4rem;">
View code
</p>
</i>
</a>
</div>
</td>
</tr>
<tr>
<td>
Using the finally block
</td>
<td>
<div class="btn-container-center">
<a class="btn-link btn-link-color1"
href="https://github.com/tridib2003/levelupjava-wiki/blob/main/Exception%20Handling/FinallySample.java"
target="_blank">
<i class="fa fa-github" style="font-size: 18px; color: whitesmoke;">
<p style="display: inline; margin-left: 0.4rem;">
View code
</p>
</i>
</a>
</div>
</td>
</tr>
</table>
</div>
<br><br><br>
<footer class="navigation-std">
<ul class="ul-remove-bullet nav-pills">
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Previous</a>
</li>
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Home</a>
</li>
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Next</a>
</li>
</ul>
</footer>
</body>
</html>