Chapter Summary


[Page 503 (continued)]

Technical Terms

catch an exception

catch block

checked exception

dialog box

dynamic scope

error dialog

exception

exception handling

finally block

method call stack

method stack trace

modal dialog

static scope

throw an exception

TRy block

unchecked exception

Summary of Important Points

  • In Java, when an error or exceptional condition occurs, you tHRow an Exception, which is caught by a special code known as an exception handler. A throw statementthrow new Exception()is used to throw an exception.


  • [Page 504]
  • A try block is a block of statements containing one or more statements that may throw an exception. Embedding a statement in a try block indicates your awareness that it might throw an exception and your intention to handle the exception.

  • Java distinguishes between checked and unchecked exceptions. Checked exceptions must either be caught by the method in which they occur or you must declare that the method containing that statement throws the exception.

  • Unchecked exceptions are those that belong to subclasses of RuntimeException. If they are left uncaught, they will be handled by Java's default exception handlers.

  • A catch block is a block of statements that handles the exceptions that match its parameter. A catch block can only follow a try block, and there may be more than one catch block for each try block.

  • The try/catch syntax allows you to separate the normal parts of an algorithm from special code meant to handle errors and exceptional conditions.

  • A method stack trace is a trace of the method calls that have led to the execution of a particular statement in the program. The Exception.printStackTrace() method can be called by exception handlers to print a trace of exactly how the program reached the statement that threw the exception.

  • Static scoping refers to how the text of the program is arranged. If a variable is declared within a method or a block, its static scope is confined to that method or block.

  • Dynamic scoping refers to how the program is executed. A statement is within the dynamic scope of a method or block if it is called from that method or block, or if it is called by some other method that was called from that method or block.

  • When searching for a catch block to handle an exception thrown by a statement, Java searches upward through the statement's static scope and backward through its dynamic scope until it finds a matching catch block. If none is found, the Java Virtual Machine will handle the exception itself by printing an error message and a method stack trace.

  • Many Java library methods throw exceptions when an error occurs. These tHRow statements do not appear in the program. For example, Java's integer division operator will throw an ArithmeticException if an attempt is made to divide by zero.

  • Generally, there are four ways to handle an exception: (1) let Java handle it; (2) fix the problem that led to the exception and resume the program; (3) report the problem and resume the program; and (4) print an error message and terminate the program. Most erroneous conditions reported by exceptions are difficult or impossible to fix.

  • A finally statement is an optional part of a try/catch block. Statements contained in a finally block will be executed whether an exception is raised or not.

  • A well-designed program should use exception handling to deal with truly exceptional conditions, not as a means of normal program control.

  • User-defined exceptions can be defined by extending the Exception class or one of its subclasses.




Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net