Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Programming Fifth Edition

Similar presentations


Presentation on theme: "Java Programming Fifth Edition"— Presentation transcript:

1 Java Programming Fifth Edition
Chapter 11 Exception Handling

2 Learning About Exceptions
Unexpected or error conditions Causes Call to file that does not exist Try to write to full disk User enters invalid data Program attempts to divide value by 0 Invalid array subscript Performing illegal arithmetic operations Java Programming, Fifth Edition

3 Learning About Exceptions (continued)
Do not necessarily have to deal with Exception Let the offending program terminate Abrupt and unforgiving Use a decision to avoid an error, no exception-handling techniques needed. Can be complicated (e.g. valid file name). Java Programming, Fifth Edition

4 Learning About Exceptions (continued)
Exception handling Provides a more elegant solution for handling error conditions Fault-tolerant Designed to continue to operate when some part of system fails Robustness Represents degree to which system is resilient to stress Java Programming, Fifth Edition

5 Trying Code and Catching Exceptions
try block Segment of code in which something might go wrong Attempts to execute Acknowledging exception might occur try block includes: Keyword try Opening and closing curly brace Executable statements Which might cause exception Java Programming, Fifth Edition

6 Trying Code and Catching Exceptions (continued)
catch block Segment of code Immediately follows try block Handles exception thrown by try block preceding it Can “catch” Object of type Exception Or Exception child class throw statement Sends Exception out of method Can be handled elsewhere Java Programming, Fifth Edition

7 Trying Code and Catching Exceptions (continued)
Java Programming, Fifth Edition

8 Trying Code and Catching Exceptions (continued)
If no Exception occurs within the try block catch block does not execute getMessage() method Obtain information about Exception Within catch block Might want to add code to correct the error Java Programming, Fifth Edition

9 Trying Code and Catching Exceptions (continued)
Java Programming, Fifth Edition

10 Throwing and Catching Multiple Exceptions
Can place multiple statements within try block Only first error-generating statement throws Exception Catch multiple Exceptions Examined in sequence Until match found for Exception type Matching catch block executes Each remaining catch block bypassed Java Programming, Fifth Edition

11 Java Programming, Fifth Edition

12 Throwing and Catching Multiple Exceptions (continued)
“Catch-all” block Accepts more generic Exception argument type: catch(Exception e) Unreachable code Program statements that can never execute under any circumstances Poor style for method to throw more than three or four types Java Programming, Fifth Edition

13 Java Programming, Fifth Edition

14 Java Programming, Fifth Edition


Download ppt "Java Programming Fifth Edition"

Similar presentations


Ads by Google