|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Now that you've familiarized yourself with theListOfNumbersclass and where the exceptions can be thrown within it, you can learn how to write exception handlers to catch and handle those exceptions.The three sections that follow cover the three components of an exception handler -- the
try,catch, andfinallyblocks. They show you how to write an exception handler for theListOfNumbersclass'swriteListmethod, described in The ListOfNumbers Example. Next, you'll walk through the resultingwriteListmethod and see what occurs within the example code during various scenarios.
try BlockThe first step in writing an exception handler is to enclose the statements that might throw an exception within atryblock. Thetryblock is said to govern the statements enclosed within it and defines the scope of any exception handlers (established by subsequentcatchblocks) associated with it.
catch Block(s)Next, you associate exception handlers with atryblock by providing one or morecatchblocks directly after thetryblock.
finally BlockJava'sfinallyblock provides a mechanism that allows your method to clean up after itself regardless of what happens within thetryblock. Use thefinallyblock to close files or release other system resources.
The previous sections describe how to construct thetry,catch, andfinallycode blocks for thewriteListexample. Now, let's walk through the code and investigate what happens during three scenarios.
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.