Page 626 - Introduction to Programming with Java: A Problem Solving Approach
P. 626

                592 Chapter 14 Exception Handling Robustness and the getInsets Method
In the LinePlotPanel constructor in Figure 14.21a, note how frame (the program’s output window) calls the getInsets method. The getInsets method returns a window’s Insets object. The Insets object stores the thicknesses of the window’s four borders. For example, frame.getInsets().left returns the width (in pixels) of the frame window’s left border and frame.getInsets().top returns the height (in pixels) of the frame window’s top border. The top border includes the height of the title bar.
If you don’t want to bother with the getInsets method, you might be tempted to use hard-coded guesses for the border sizes. Don’t do it. Different Java platforms (e.g., Windows, UNIX, and Macintosh platforms) have different window border sizes. So even if you guess right for your current Java platform, your guesses won’t necessarily work for alternative Java platforms. Moral of the story: Be robust and use getInsets. Don’t use hard-coded guesses.
Summary
• An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions during the execution of a program.
• Exception handling is a technique for handling exceptions gracefully.
• Use a try block to “try” out one or more dangerous method calls. If there’s a problem with the danger-
ous method calls, the JVM throws an exception and looks for a “matching” catch block.
• A catch block is matching if the catch heading’s parameter type is the same as or an ancestor of the
type of the thrown exception.
• If an exception is thrown, the JVM immediately jumps out of the current try block. That means that if there are statements in the try block after the exception-throwing statement, those statements get skipped.
• Checked exceptions must be checked with a try-catch mechanism.
• Unchecked exceptions may optionally be checked with a try-catch mechanism, but it’s not a
requirement.
• Unchecked exceptions are descendants of the RuntimeException class.
• To implement a simple, general-purpose exception handler, define a catch block with an Exception
type parameter, and inside the catch block, call the Exception class’s getMessage method.
• To define an exception handler with more specificity, define a sequence of catch blocks. Arrange the
catch blocks with the more general exception classes at the bottom.
• If a program crashes, the JVM prints a call stack trace. A call stack trace is a listing of the methods that
were called prior to the crash, in reverse order.
• Use a throws clause to propagate an exception back to the calling module.
• If you handle an exception with a throws clause, and you need to provide clean up code regardless of
whether an exception is thrown, use a finally block. Review Questions
§14.3 Using try and catch Blocks to Handle “Dangerous” Method Calls
1. If your program contains an API method call, you should put it inside a try block. To be fully compliant with proper coding practice, you should apply this rule for all your API method calls. (T / F)
 Apago PDF Enhancer
 







































































   624   625   626   627   628