Presentation is loading. Please wait.

Presentation is loading. Please wait.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 8 - 1 Chapter 8 Exception Handling Sections 1-5, 7.

Similar presentations


Presentation on theme: "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 8 - 1 Chapter 8 Exception Handling Sections 1-5, 7."— Presentation transcript:

1 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 8 - 1 Chapter 8 Exception Handling Sections 1-5, 7

2 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 8 - 2 Definition An exception represents an error condition that occurs during the normal course of program execution. –Exceptions are error conditions that are not the fault of the programmer When an exception occurs, the normal sequence of flow is terminated. –We say an exception is thrown When an exception-handling routine is executed, we say the exception is caught.

3 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 8 - 3 Programming with Exceptions Programmer has control over what happens when an exception is thrown –do nothing => program will quit –can add code to recover from the exception where it occurs in any method on the call stack when the exception is thrown Program can create and throw exceptions when errors occur You can create your own exception classes –Extend Exception or RuntimeException

4 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 8 - 4 Ignoring Exceptions String inputStr; int age; inputStr = JOptionPane.showInputDialog(null, "Age:"); age = Integer.parseInt(inputStr); java.lang.NumberFormatException: ten at java.lang.Integer.parseInt(Integer.java:405) at java.lang.Integer.parseInt(Integer.java:454) at Ch8Sample1.main(Ch8Sample1.java:20) Error message for invalid input

5 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 8 - 5 Catching an Exception trycatch inputStr = JOptionPane.showInputDialog(null, "Age:"); try { age = Integer.parseInt(inputStr); } catch (NumberFormatException e){ JOptionPane.showMessageDialog(null, "’" + inputStr + "‘ is invalid\n" + "Please enter digits only"); }

6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 8 - 6 try-catch Control Flow

7 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 8 - 7 Exception Methods Two methods give information about the exception: –getMessage returns a String describing the error –printStackTrace returns a list of all the methods on the call stack and the line numbers being executed try {... } catch (NumberFormatException e){ System.out.println(e.getMessage()); e.printStackTrace(); }

8 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 8 - 8 Multiple catch Blocks Single try-catch statement can have different catch blocks for each potential exception type. try {... age = Integer.parseInt(inputStr);... val = cal.get(id); //cal is a GregorianCalendar... } catch (NumberFormatException e){... } catch (ArrayIndexOutOfBoundsException e){... }

9 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 8 - 9 Multiple catch Control Flow

10 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 8 - 10 The finally Block Occasionally, we need to take certain actions regardless of whether an exception is thrown. We can place statements that must be executed in a finally block. finally block executes before the method returns regardless of what else happens.

11 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 8 - 11 try-catch-finally Control Flow

12 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 8 - 12 Propagating Exceptions Instead of catching an exception, we can send the exception back to the caller of our method. –Use the reserved word throws in the method header. public int getAge( ) throws NumberFormatException {... int age = Integer.parseInt(inputStr);... return age; }

13 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 8 - 13 Throwing Exceptions We can write a method that creates and throws an exception. Use the throw reserved to propogate the Exception. The method header includes the reserved word throws. public void doWork(int num) throws Exception {... if (num != val) throw new Exception("Invalid value");... }

14 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 8 - 14 Sample Call Sequence

15 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 8 - 15 Exception Types All types of thrown errors are instances of the Throwable class or its subclasses. Serious errors are represented by instances of the Error class or its subclasses. –Errors are not usually handled Exceptions that applications should handle are represented by the Exception class or its subclasses. There are over 60 classes in the hierarchy.

16 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 8 - 16 Checked vs. Runtime Exceptions A checked exception is an exception that is checked by the compiler. –The compiler requires that you either have a try-catch statement or a throws clause –ParseException is a checked exception All other exceptions are unchecked, or runtime, exceptions. As the name suggests, they are detected only at runtime. –You don't have to handle runtime exceptions unless you want to.

17 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 8 - 17 Handling Checked Exceptions

18 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 8 - 18 Programmer-Defined Exceptions Using the standard exception classes, we can use getMessage to retrieve an error message. By defining our own exception class, we can pack more useful information –for example, we might define an OutOfStock exception class and include information such as how many items to order Example –AgeInputException, a subclass of Exception, includes three pieces of information lower bound upper bounds the (invalid) value entered by the user


Download ppt "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 8 - 1 Chapter 8 Exception Handling Sections 1-5, 7."

Similar presentations


Ads by Google