Docsity

Exception Handling - Java Programming Language - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Exception Handling - Java Programming Language - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Chapter 11: Exception Handling

  • Exceptions and Exception Types
  • Claiming Exceptions
  • Throwing Exceptions
  • Catching Exceptions
  • Rethrowing Exceptions
  • The finally Clause
  • Cautions When Using Exceptions
  • Creating Your Own Exception Classes

(Optional)

Exceptions and Exception Types

LinkageError

Error

AWTError

AWTException

Throwable

ClassNotFoundException

VirtualMachineError

IOException

Exception

RuntimeException

Object

ArithmeticException

NullPointerException

IndexOutOfBoundsException

Several more classes

Several more classes

Several more classes

Error: internal system

errors

Exception: errors

caused by your

program

Claiming Exceptions

• public void myMethod() throws IOException

– Claim that myMethod may throw an IOexception

• public void myMethod() throws IOException,

OtherException

Throwing Exceptions

• throw new TheException();

• TheException e = new TheException();

throw e;

Catching Exceptions

try

statements;

catch (Exception1 e)

{ handler for exception1 }

catch (Exception2 e)

{ handler for exception2 }

catch (ExceptionN e)

{ handler for exceptionN }

Catching Exceptions

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

}

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

}

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

}

What will happen if method3 throws

• an Exception3,

• an Exception2,

• an Exception1, or

• no of the above exceptions?

Example 11.2 Exceptions in

GUI Applications

• An error message appears on the console,

but the GUI application continues running.

• Re-run the MenuDemo applet from Example

9.9 and divide by 0 to see how a GUI deals

with unhandled exceptions.

MenuDemo Run

Rethrowing Exceptions

try

statements;

catch(TheException e)

perform operations before exits;

throw e;

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.

Example 11.3 Creating Your Own

Exception Classes

 Objective: This program creates a Java

applet for handling account transactions. The

applet displays the account id and balance,

and lets the user deposit to or withdraw from

the account. For each transaction, a message

is displayed to indicate the status of the

transaction: successful or failed. In case of

failure, the failure reason is reported.