Skip to content

Commit fa7a6ed

Browse files
authored
Merge pull request #2 from tridib2003/WIP-Exception_Handling-2
Add: Code links
2 parents d5fe2c6 + 5ac0a56 commit fa7a6ed

File tree

1 file changed

+123
-2
lines changed

1 file changed

+123
-2
lines changed

exception-handling.html

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ <h1 class="h1-basic">Exception Handling</h1>
7575
<li>
7676
<p>
7777
<strong>throws</strong> - any exception which is thrown out of a method must be
78-
specified by
79-
a <i>throws</i> clause.
78+
specified using the <i>throws</i> clause.
8079
</p>
8180
</li>
8281
<li>
@@ -163,6 +162,52 @@ <h1 class="h1-basic">Exception Handling</h1>
163162
<i>try-catch</i> module.
164163
</p>
165164
</li>
165+
166+
<li>
167+
<p>
168+
If a block of code can raise multiple types of exceptions, we can specify multiple <i>catch</i>
169+
clauses to handle these different exceptional cases. As soon as an exception is thrown, each
170+
<i>catch</i> clause is inspected in order and once a match is found, statements within that
171+
<i>catch</i> block will be executed and others will be skipped and the control passes to the
172+
statement immediately after the <i>try-catch</i> block. Also, any exception subclass must come
173+
before its superclass, otherwise a subclass exception type would never be reached and resulting in
174+
compile time error.
175+
</p>
176+
</li>
177+
178+
<li>
179+
<p>
180+
The <strong>throw</strong> statement is used to explicitly throw an exception. The keyword
181+
<i>throw</i> is followed by an object of type <i>Throwable</i>, or a subclass of <i>Throwable</i>.
182+
We can create a <i>Throwable</i> object either by using a parameter in a <i>catch</i> clause or via
183+
creating one with the <i>new</i> operator. The execution stops immediately a <i>throw</i> statement
184+
is encountered and statements following it are not executed. Then the nearest <i>try</i> block is
185+
looked upon to find match for the exception type, if found then those statements are executed. If no
186+
such matching <i>catch</i> block is found the default exception handler stops the program execution
187+
and prints stack trace.
188+
</p>
189+
</li>
190+
191+
<li>
192+
<p>
193+
The <strong>throws</strong> clause is put after the parameter-list in the method's declaration, if a
194+
method can cause exceptions that it does not take care of. After the <i>throws</i> clause the list
195+
of all exceptions types that the method can throw are mentioned. If the <i>throws</i> clause is
196+
missed out, it will result in a compile-time error. All types of exceptions, except those of type
197+
<strong>Error</strong> or <strong>RuntimeException</strong>, or any of their subclasses must be
198+
declared in the <i>throws</i> statement.
199+
</p>
200+
</li>
201+
202+
<li>
203+
<p>
204+
Statements within the <strong>finally</strong> will be executed whether or not an exception is
205+
thrown. The <i>finally</i> block is executed just before the control returns to the caller from the
206+
<i>try-catch</i> block. Although the <i>finally</i> clause if optional, each <i>try</i> statement
207+
must have at least one <i>catch</i> or a <i>finally</i> clause.
208+
</p>
209+
</li>
210+
166211
</ul>
167212
</div>
168213

@@ -215,6 +260,82 @@ <h1 class="h1-basic">Exception Handling</h1>
215260
</td>
216261
</tr>
217262

263+
<tr>
264+
<td>
265+
Handling exceptions using multiple catch statements
266+
</td>
267+
<td>
268+
<div class="btn-container-center">
269+
<a class="btn-link btn-link-color1"
270+
href="https://github.com/tridib2003/levelupjava-wiki/blob/main/Exception%20Handling/MultipleCatches.java"
271+
target="_blank">
272+
<i class="fa fa-github" style="font-size: 18px; color: whitesmoke;">
273+
<p style="display: inline; margin-left: 0.4rem;">
274+
View code
275+
</p>
276+
</i>
277+
</a>
278+
</div>
279+
</td>
280+
</tr>
281+
282+
<tr>
283+
<td>
284+
Explicitly throw exceptions using the throw statement
285+
</td>
286+
<td>
287+
<div class="btn-container-center">
288+
<a class="btn-link btn-link-color1"
289+
href="https://github.com/tridib2003/levelupjava-wiki/blob/main/Exception%20Handling/ThrowSample.java"
290+
target="_blank">
291+
<i class="fa fa-github" style="font-size: 18px; color: whitesmoke;">
292+
<p style="display: inline; margin-left: 0.4rem;">
293+
View code
294+
</p>
295+
</i>
296+
</a>
297+
</div>
298+
</td>
299+
</tr>
300+
301+
<tr>
302+
<td>
303+
Specify exceptions that might occur using the throws clause
304+
</td>
305+
<td>
306+
<div class="btn-container-center">
307+
<a class="btn-link btn-link-color1"
308+
href="https://github.com/tridib2003/levelupjava-wiki/blob/main/Exception%20Handling/ThrowsSample.java"
309+
target="_blank">
310+
<i class="fa fa-github" style="font-size: 18px; color: whitesmoke;">
311+
<p style="display: inline; margin-left: 0.4rem;">
312+
View code
313+
</p>
314+
</i>
315+
</a>
316+
</div>
317+
</td>
318+
</tr>
319+
320+
<tr>
321+
<td>
322+
Using the finally block
323+
</td>
324+
<td>
325+
<div class="btn-container-center">
326+
<a class="btn-link btn-link-color1"
327+
href="https://github.com/tridib2003/levelupjava-wiki/blob/main/Exception%20Handling/FinallySample.java"
328+
target="_blank">
329+
<i class="fa fa-github" style="font-size: 18px; color: whitesmoke;">
330+
<p style="display: inline; margin-left: 0.4rem;">
331+
View code
332+
</p>
333+
</i>
334+
</a>
335+
</div>
336+
</td>
337+
</tr>
338+
218339
</table>
219340

220341
</div>

0 commit comments

Comments
 (0)