Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.

Similar presentations


Presentation on theme: "Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch."— Presentation transcript:

1 Java Programming Exceptions Handling

2 Topics: 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 Throwable Exceptions (Checked/unchecked) AWTErrorThreadDeath Error (Unchecked) RuntimeException (Unchecked) IOException (Checked) Exception Class Hierarchy OutOfMemory Checked Exception: “must catch or declare”

7 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

8 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

9 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

10 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

11 Creating a catch block Create a catch block 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

12 General format of a try…catch pair

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

14 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

15 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

16

17

18 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

19

20 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

21

22

23 Specifying the Exceptions a Method can Throw When you write a method that might throw an Exception, you can type the clause throws 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

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

25 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

26 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

27 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


Download ppt "Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch."

Similar presentations


Ads by Google