java programming exceptions handling. topics: learn about exceptions try code and catch exceptions...

Post on 27-Dec-2015

224 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Java Programming

Exceptions Handling

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

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

Learning about Exceptions

Exception- An unexpected or error conditionFor 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

Learning about Exceptions

Exception Handling- The object-oriented techniques to manage such errors comprise the group of methods know as exception handlingThere are two basic classes of errors

Error and Exception

Both classes descend from the Throwable classYou must expect the unexpected

Throwable

Exceptions(Checked/unchecked)

AWTError ThreadDeath

Error(Unchecked)

RuntimeException(Unchecked)

IOException(Checked)

Exception Class Hierarchy

OutOfMemoryChecked Exception: “must catch or declare”

Learning about Exceptions

Error class- Represents more serious errors from which your program usually cannot recoverFor example, spelling a class name incorrectly

or storing a required class in the wrong folder

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 recoverExamples of Exception class errors include

using an invalid array subscript or performing certain illegal arithmetic operations

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 tryAn opening curly braceStatements that might cause exceptionsA closing curly brace

Trying Code and Catching Exceptions catch block- Must code at least one catch

block immediately following a try blockA segment of code that can handle an

Exception that might be thrown by the try block that precedes it

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

General format of a try…catch pair

Using the Exception GetMessage() Method getMessage() method- To retrieve Java's

message about any Throwable Exception named someExceptionCode someException.getMessage()

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

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

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

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

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

Specifying the Exceptions a Method can Throw Method's signature- A method's header,

including its name, any arguments, and any throws clause

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

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

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

top related