Download presentation
Presentation is loading. Please wait.
1
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8 Errors and Exceptions Throwable class Handling Exceptions Throwing Exceptions Exception propagation
2
©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Exceptions An exception represents an error condition that can occur during the normal course of program execution. When an exception occurs, or is thrown, the normal sequence of flow is terminated. The exception-handling routine is then executed; we say the thrown exception is caught. see Ch8Sample1
3
©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Throwable Class In Java an exception is represented as an instance of the Throwable class or its subclasses. The Throwable class has two subclasses: Error - the Error class represents serious problems that should not be caught by ordinary applications. Exception - the Exception class represents error conditions that can be caught and handled.
4
©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Handling Exceptions Until now, we've allowed Exceptions to terminate our programs. Programs are much more reliable and robust if we put in code that allows the program to recover from Exceptions. One way to do this is to wrap the statements that may throw an exception with the try-catch control statement.
5
©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 The try-catch statement Try-catch is a kind of control statement that allows us to control what happens when an Exception occurs. try catch (Exception e) We must specify which exception we are catching in the catch block’s parameter list.
6
©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Example inputStr = JOptionPane.showInputDialog(null, prompt); try { age = Integer.parseInt(inputStr); } catch (NumberFormatException e){ JOptionPane.showMessageDialog(null, "’" + inputStr +‘ is invalid\n" +"Please enter digits only");
7
©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Execution Sequence Statements in the try block are executed in sequence. When one of the statements throws an exception, control is passed to the matching catch block and statements inside the catch block are executed. The execution then continues to the statement following the try-block statement, ignoring any remaining statements in the try block. If no statements in the try block throw an exception, the catch block is ignored. Execution continues with the statement following the try-catch statement.
8
©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 try-catch Control Flow try-catch statement with one catch block.
9
©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Throwable Methods There are two methods of the Throwable class we can call to get information about the thrown exception: getMessage - returns a description of the problem printStackTrace - prints the sequence of methods called to get to the line where the error occurred
10
©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Run-Time Stack The run-time stack is what is used to keep track of the methods that are currently being executed. When you call a method, the system puts the parameter data and the location the method was called from on the top of a data structure called a stack. When the method finishes, this data is removed from the stack and the calling method continues at the line the method was called from.
11
©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Throwing Exceptions An exception can be thrown in a program using the throw statement. throw where is an instance of the Throwable class or its subclasses. This allows you to alter the flow of your program by creating and throwing Exceptions
12
©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Catching Exceptions A try statement can have multiple catch blocks. When there are multiple catch blocks in a try- catch statement, they are checked in sequence. It is important to check more specialized exception classes before the more general exception classes. When an exception is thrown, its matching catch block is executed and the other catch blocks are ignored. Alternately, you can have a single catch block and use the instanceof operator to determine what type of Exception was thrown.
13
©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 try-catch with multiple catch blocks
14
©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Which catch Block? If a catch block matches the Exception, it will be executed. If none of the catch blocks matches the thrown exception, the system will search down the stack trace for a method with a matching catch block. If no matching catch block is found, the system will handle the thrown exception.
15
©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 The finally Clause If there is a block of code that must be executed regardless of whether an exception is thrown, we use the reserved word finally. The finally block is executed even if there is a return statement inside the try block. When the return statement is encountered in the try block, statements in the finally block are executed before actually returning from the method.
16
©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 try-catch with finally
17
©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Propagating Exceptions It doesn't always make sense to handle an exception in the method where it occurs. Different users of a class may want to handle the same Exception differently. Do not catch an exception that is thrown as a result of violating a condition set by the client programmer. Instead, propagate the exception back to the client programmer’s code and let him or her handle it.
18
©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Propagating Exceptions When a method may throw an exception, either directly or indirectly, we call the method an exception thrower. Every exception thrower must be one of two types: An exception catcher is an exception thrower that includes a matching catch block for the thrown exception. An exception propagator does not contain a matching catch block. A method may be a catcher of one exception and a propagator of another.
19
©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Exception Propagation public A() { try { B() } catch (Exception e) { … } } public B() { try { C() } catch (Exception e) { … } } public C() { D();} public D() { if (cond)throw new Exception(); }
20
©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Types of Exceptions There are two types of exceptions: Checked. Unchecked. A checked exception is an exception that is checked at compile time. All other exceptions are unchecked exceptions, Also known as runtime exceptions.
21
©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 throws Clause For a propagator of checked exceptions, we need to modify its header to declare the type of exceptions the method propagates. We use the reserved word throws for this declaration. void C( ) throws Exception {... } Without the required throws Exception clause, the program will not compile. For runtime exceptions, the throws clause is optional.
22
©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Programmer-Defined Exceptions Instead of using generic exception classes, we can define our own exception classes and attach useful information to the exception objects. When creating a new customized exception class, define it as a checked exception to ensure client programmers handle thrown exceptions of this class in their code.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.