1 / 26

Java Programming, Second Edition

Java Programming, Second Edition. Chapter Fifteen Exception Handling. In this chapter, you will:. Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch multiple Exceptions Use the finally block

meira
Télécharger la présentation

Java Programming, Second Edition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Programming, Second Edition Chapter Fifteen Exception Handling

  2. In this chapter, you will: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch multiple Exceptions Use the finally block Understand the limitations of traditional error handling

  3. Specify the Exceptions a method can throw • Handle Exceptions uniquely with each catch • Trace Exceptions through the call stack • Create your own Exceptions

  4. Learning about Exceptions • Exception- An unexpected or error condition • For example: • You issue a command to read a file from a disk, but the file does not exist • You write data to a disk but the disk is full or unformatted • The program attempts to divide by zero

  5. Learning about Exceptions • Exception Handling- The object-oriented techniques to manage such errors comprise the group of methods know as exception handling • There are two basic classes of errors • Error and Exception • Both classes descend from the Throwable class • You must expect the unexpected

  6. Learning about Exceptions • Error class- Represents more serious errors from which your program usually cannot recover • For example, spelling a class name incorrectly or storing a required class in the wrong folder

  7. Learning about Exceptions • Exception class- Comprise less serious errors that represent unusual conditions that arise while a program is running, and from which the program can recover • Examples of Exception class errors include using an invalid array subscript or performing certain illegal arithmetic operations

  8. Trying Code and Catching Exceptions • try block- A block of code you attempt to execute, while acknowledging that an Exception might occur • Consists of the following elements: • The keyword try • An opening curly brace • Statements that might cause exceptions • A closing curly brace

  9. Trying Code and Catching Exceptions • catch block- Must code at least one catch block immediately following a try block • A segment of code that can handle an Exception that might be thrown by the try block that precedes it

  10. Creating a catch block • Create a catch blocky by typing the following elements: • The keyword catch • An opening parenthesis • An Exception type • A name for an instance of the Exception type • A closing parenthesis • An opening curly brace • Statements that take the action you want to use to handle the error condition • A closing curly brace

  11. General format of a try…catch pair

  12. Using the Exception GetMessage() Method • getMessage() method- To retrieve Java's message about any Throwable Exception named someException • Code someException.getMessage()

  13. Throwing and Catching Multiple Exceptions • Can place as many statements as you need within a try block • Can catch as many Exceptions as you want • If you try more than one statement, only the first error-generating statement throws an Exception • As soon as the Exception occurs, the logic transfers to the catch block

  14. Throwing and Catching Multiple Exceptions • Multiple catch blocks are examined in sequence until a match is found for the type of Exception that occurred • The match catch block executes and each remaining catch block is bypassed

  15. Using the finally Block • The finally block contains actions you must perform at the end of a try…catch sequence • Code within a finally block executes whether or not the preceding try block identifies exceptions • The finally block performs clean-up tasks that must happen

  16. Understanding the Limitations of Traditional Error Handling • Before object-oriented programming, potential errors were handled using error-prone methods • Object-oriented exception handling allows you to isolate error-handling code

  17. Specifying the Exceptions a Method can Throw • When you write a method that might throw an Exception, you can type the clause throws <name> Exception after the method header to indicate the type of exception that might be thrown • When you use any method you must know • The method’s name • The method’s return type • The type and number of arguments it requires • The type and number of Exceptions the method throws

  18. Specifying the Exceptions a Method can Throw • Method's signature- A method's header, including its name, any arguments, and any throws clause

  19. Handling Exceptions Uniquely with Each catch • When you use a class and one of its methods throws an Exception, the class that throws the Exception does not have to catch it • Your calling program can catch the Exception, and then you can decide what to do with it

  20. Tracing Exceptions Through the Call Stack • Call stack- Where the computer stores the list of locations to which the system must return • When a method throws an Exception, and if the method does not catch the Exception, the Exception is thrown to the next method up the call stack • You can display this list using the printStackTrace() method

  21. Creating Your Own Exceptions • Java provides over 40 categories of Exceptions that you can throw in your programs • Java also allows you to create your own Exceptions • You should extend your Exceptions from the Exception class • When you create an Exception, it's conventional to end its name with Exception

More Related