Unformatted text preview:

Slide 1Slide 211.1 IntroductionA review of what we saw before …Slide 5The client application Fig. 8.2 (TimeTest.java)Slide 7Slide 8Slide 9Some sample exceptions we’ve seen …The Throwable classException Handling and the Call StackThe Call StackThe Catch or Specify Requirement (aka. Catch or Declare)Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 2711.5 Java Exception HierarchySlide 29The chained exceptionSlide 31Slide 32Slide 33Slide 34Slide 3511.6 The finally BlockSlide 37Slide 38Slide 39Slide 40Slide 41Slide 42Slide 43Slide 44Slide 45Slide 46Slide 47Slide 48Slide 49Slide 5011.9 Declaring New Exception TypesSlide 52Slide 53Slide 54Exception Handling1-Based on slides from Deitel & Associates, Inc.- Revised by T. A. Yang2311.1 Introduction•Exception — an indication of a problem that occurs during a program’s execution. –The name “exception” implies that the problem occurs infrequently. •http://download.oracle.com/javase/tutorial/essential/exceptions/definition.html:−" The term exception is shorthand for the phrase exceptional event.“−Definition: An exception is an event, which occurs during the execution of a program and disrupts the normal flow of the program's instructions.•With exception handling, a program can continue executing (rather than terminating) after dealing with the exception. –Mission-critical or business-critical computing.–Robust and fault-tolerant programs (i.e., programs that can deal with problems as they arise and continue executing).4A review of what we saw before …56The client applicationFig. 8.2 (TimeTest.java)78•Questions regarding exception handling in the sample application:Q1: Which method in which class would throw the IllegalArgumentException?Q2: Under what condition would the exception be thrown?Q3: How would the client catch the exception when it is thrown?9 Method setTime() and Exception Handling•For incorrect values, setTime throws an exception of type IllegalArgumentException (lines 23–24)•Notifies the client code that an invalid argument was passed to the method. Q: Who’s the client of setTime() in this application?•The throw statement (line 23) creates a new object of type IllegalArgumentException. In this case, we call the constructor that allows us to specify a custom error message. •After the exception object is created, the throw statement immediately terminates method setTime() and the exception is returned to the client code that attempted to set the time.•The client can use try...catch to catch exceptions and attempt to recover from them.10Some sample exceptions we’ve seen …•ArrayIndexOutOfBoundsException occurs when an attempt is made to access an element past either end of an array. •A NullPointerException occurs when a null reference is used where an object is expected. •ClassCastException occurs when an attempt is made to cast an object that does not have an is-a relationship with the type specified in the cast operator. •ClassNotFoundException, IOException, …For a list of subclasses of Exception, see http://download.oracle.com/javase/6/docs/api/java/lang/Exception.html.11•The Throwable class is the superclass of all errors and exceptions in the Java language. −An Error indicates serious problems that a reasonable application should not try to catch. (http://download.oracle.com/javase/6/docs/api/java/lang/Error.html)−The class Exception and its subclasses indicates conditions that a reasonable application might want to catch. (http://download.oracle.com/javase/6/docs/api/java/lang/Exception.html)•Only objects that are instances of Throwable (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. •Similarly, only Throwable or one of its subclasses can be the argument type in a catch clause. The Throwable classhttp://download.oracle.com/javase/6/docs/api/java/lang/Throwable.html12•Exceptions are thrown (i.e., the exception occurs) when a method detects a problem and is unable to handle it. •Stack trace — information displayed when an exception occurs and is not handled. Information includes:–The name of the exception in a descriptive message that indicates the problem that occurred –The method-call stack (i.e., the call chain) at the time it occurred. Represents the path of execution that led to the exception method by method.Exception Handling and the Call StackThe Call Stack13Source: http://download.oracle.com/javase/tutorial/essential/exceptions/definition.html •main( )  m1( )  m2( )  m3( )The Catch or Specify Requirement(aka. Catch or Declare)•From: http://download.oracle.com/javase/tutorial/essential/exceptions/catchOrDeclare.htmlValid Java code must honor the Catch or Specify Requirement. This means that code that might throw certain exceptions must be enclosed by either of the following: a) A try statement that catches the exception. The try must provide a handler for the exception. e.g., m1( )b) A method that specifies that it can throw the exception. The method must provide a throws clause that lists the exception. e.g., m3( )1415161711.3 Example: Handling A rithmeticExceptions and InputMismatchExceptions•The application in Fig. 11.2 uses exception handling to process any ArithmeticExceptions and InputMistmatchExceptions that arise. •If the user makes a mistake, the program catches and handles the exception — in this case, allowing the user to try to enter the input again.181920•Q: What code should be enclosed within the try block?–code that might throw an exception –code that should not execute if an exception occurs•The catch block (also called a catch clause or exception handler) catches and handles an exception. –Begins with the keyword catch and is followed by an exception parameter in parentheses and a block of code enclosed in curly braces. •At least one catch block or a finally block (Section 11.6) must immediately follow the try block.21•The exception parameter in the catch block identifies the exception type the handler can process. –The parameter’s name enables the catch block to interact with a caught exception object. •When an exception occurs in a try block, the catch block that executes is the first one whose type matches the type of the exception that occurred.•Use the System.err (standard error stream) object to output error messages. –By default, displays


View Full Document

UHCL CSCI 3134 - Exception Handling

Download Exception Handling
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Exception Handling and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Exception Handling 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?