lecture10 exception handling

22
Lecture10 Exception Handling Jaeki Song

Upload: camdyn

Post on 24-Jan-2016

86 views

Category:

Documents


4 download

DESCRIPTION

Lecture10 Exception Handling. Jaeki Song. Introduction. Categories of errors Compilation error The rules of language have not been followed Runtime error The program is running if the environment detects an operation that is impossible to carry out Logic error - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture10 Exception Handling

Lecture10Exception Handling

Jaeki Song

Page 2: Lecture10 Exception Handling

Introduction

• Categories of errors– Compilation error

• The rules of language have not been followed

– Runtime error• The program is running if the environment detects

an operation that is impossible to carry out

– Logic error• A program doesn’t perform the way it was intended

to

Page 3: Lecture10 Exception Handling

Exception

• Runtime errors causes exceptions– Events that occur during the execution of a program

and disrupt the normal flow of control• A program that does not provide the code to handle exceptions

may terminate abnormally, causing serious problem

• E.g.– The user may enter invalid input

– The program may attempt to open file that doesn’t exist

– The program may attempt to access an out-of-bound array element

Page 4: Lecture10 Exception Handling

Java and Exception

LinkageError

Error

AWTError

AWTException

Throwable

ClassNotFoundException

VirtualMachineError

IOException

Exception

RuntimeException

Object

ArithmeticException

NullPointerException

IndexOutOfBoundsException

Several more classes

Several more classes

Several more classes

Page 5: Lecture10 Exception Handling

Java and Exception

• A java exception is an instance of a class derived from Throwable– Errors related to graphic are included in the

java.awt package– Numeric exceptions are included in the

java.lang package

Page 6: Lecture10 Exception Handling

Exception Classes

• The Exception class describes errors caused by your program and external circumstances– ClassNotFoundException

• If you attempt to use a class that does not exist

– RuntimeException• Describes programming errors, such as bad casting,

accessing an out-of-bound array, and numeric errors

Page 7: Lecture10 Exception Handling

Common Exceptions

Exception Purpose

ArithmetricException Error caused by a calculation, such as division by zero

NumberFormatExceptionProblem converting a string to a number; occurs when thetext field is blank or contains a fraction when an integer is required

IllegalArgumentExceptionUnable to format the value passed to one of the format methods

FileNotFoundException File does not exist in path specified

IOExceptionFailure of an input or output operation such as reading froma file

OutOfMemoryException Not enough memory to create an object

Page 8: Lecture10 Exception Handling

Exception Handling

• The exception handling in Java consists of claiming exception, throwing exception, and catching and processing exceptions

method1() { try { invoke method2; } catch (Exception ex) { Process exception; } }

method2() throws Exception { if (an error occurs) { throw new Exception(); } }

catch exception throw exception

claim exception

Page 9: Lecture10 Exception Handling

Claiming Exceptions

• In general, every method states the types of exceptions it might encounter– Simply tells the compiler what might go wrong when

the method is executing

• To claim an exception in a method, you use the throws keywordpublic void myMethod( ) throws IOException

public void myMethod( ) throws IOException, OtherExceptions

Page 10: Lecture10 Exception Handling

Throwing Exceptions

• When a statement causes errors, the method containing the statement creates an exception object and passes it to the system– The exception object contains information about

exception, including its type when the error occurred

• In the method that has claimed the exception, the following is the syntax to throw an exception:TheException ex = new TheException( );

throw ex

Page 11: Lecture10 Exception Handling

Catching Exceptions

• After a method throws an exception, the Java runtime systems begins the process of finding the code to handle the error– The code that handles the error is called the

exception handler– The handler must match the type of exception

thrown• If no handler is found, the program terminates

Page 12: Lecture10 Exception Handling

Catching Exceptions

• When calling a method that explicitly claims an exception, you must use the try-catch block

try{ statement; } //statements that may throw exceptions

catch (Exception1 ex){ handler for exception1;}

catch (Exception2 ex){handler for exception2;}

Page 13: Lecture10 Exception Handling

Catching Exceptions

• If no exception arise during the execution of the try clause, the catch clauses are skipped

• If one of the statement inside the try block throws an exception, Java skips the remaining statements and starts to search for a handler for the exception

Page 14: Lecture10 Exception Handling

Catching Exceptions

main method { ... try { ... invoke method1; statement1; } catch (Exception1 ex1) { Process ex1; } }

method1 { ... try { ... invoke method2; statement2; } catch (Exception2 ex2) { Process ex2; } }

method2 { ... try { ... invoke method3; statement3; } catch (Exception3 ex3) { Process ex3; } }

Page 15: Lecture10 Exception Handling

Example

• Example 1

• Example 2

Page 16: Lecture10 Exception Handling

The Exception Object

• If an exception of a subclass of Exception occurs in a graphics program, Java prints the error message – The exception object contains valuable

information about the exception

Page 17: Lecture10 Exception Handling

The Exception Object

• public String getMessage()– Returns the detailed message of the Throwable object

• public String toString()– Returns a short description of the Throwable object

• public void printStackTrace( )– Prints the Throwable object and its trace information

Page 18: Lecture10 Exception Handling

The finally Clause

try

{ statements;}

catch(TheException e)

{ handling e; }

finally

{ finalStatements; }

Page 19: Lecture10 Exception Handling

Cautions When Using Exceptions

• Exception handling separates error-handling code from normal programming tasks, thus making programs easier to read and to modify. Be aware, however, that exception handling usually requires more time and resources because it requires instantiating a new exception object, rolling back the call stack, and propagating the errors to the calling methods.

Page 20: Lecture10 Exception Handling

Customized Exception Handling

• Procedures– Step 1: Create own exception class by extending Exception

and creating its own constructors• Your exception class can inherit from the Exception class

public class NegativeAmountException extends Exception {

public NegativeAmountException(){ super (“Negative amount “); statement;}

}

Page 21: Lecture10 Exception Handling

Customized Exception Handling

• Step2: Throws exception when certain conditions happens

public void deposit (double amount) throws NegativeException

{

statement;

}

Page 22: Lecture10 Exception Handling

Customized Exception Handling

• Step3: Provide the try and catch clauses for the thrown exceptiontry{

statement;}catch{

statement;}