Lecture10Exceptions

Lecture10Exceptions - Exceptions Topics Detecting, throwing...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Exceptions
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Topics Detecting, throw ing and catching exceptional conditions Design your own exception
Background image of page 2
Detecting an exceptional condition public void withdraw( double amount) { if (amount > balance) { // now what??? } balance = balance – amount; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Creating an exception public void withdraw( double amount) { if (amount > balance) { IllegalArgumentException exception = new IllegalArgumentException(“Amount”+ “exceeds balance”); throw exception; } balance = balance – amount; }
Background image of page 4
Throwing an exception public void withdraw( double amount) { if (amount > balance) { IllegalArgumentException exception = new IllegalArgumentException(“Amount”+ “exceeds balance”); throw exception; } balance = balance – amount; }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Alternative throw statement public void withdraw( double amount) { if (amount > balance) { throw new IllegalArgumentException( “Amount exceeds balance”) ; } balance = balance – amount; }
Background image of page 6
Java Exceptions Page 562 in chapter 14 Examples: IOException ArithmeticException NumberFormatException IndexOutOfBoundsException NullPointerException
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Checked Exceptions There are two kinds of exceptions in java, checked and unchecked . When you call a method that throws a checked exception, you must tell the compiler what you are going to do about the exception if it is ever thrown
Background image of page 8
What happens when we throw an exception? If try/catch is in the method, control goes to the catch handler . When you throw an exception, the current method terminates immediately. If there is no handler at a higher level, the
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/13/2008 for the course CSC 216 taught by Professor Miller during the Spring '08 term at N.C. State.

Page1 / 36

Lecture10Exceptions - Exceptions Topics Detecting, throwing...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online