Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Now that you've familiarized yourself with theListOfNumbers
class 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
, andfinally
blocks. They show you how to write an exception handler for theListOfNumbers
class'swriteList
method, described in The ListOfNumbers Example. Next, you'll walk through the resultingwriteList
method 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 atry
block. Thetry
block is said to govern the statements enclosed within it and defines the scope of any exception handlers (established by subsequentcatch
blocks) associated with it.
catch
Block(s)Next, you associate exception handlers with atry
block by providing one or morecatch
blocks directly after thetry
block.
finally
BlockJava'sfinally
block provides a mechanism that allows your method to clean up after itself regardless of what happens within thetry
block. Use thefinally
block to close files or release other system resources.
The previous sections describe how to construct thetry
,catch
, andfinally
code blocks for thewriteList
example. 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-2002 Sun Microsystems, Inc. All rights reserved.