exceptions - Error Handling Traditional approach: Method...

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

View Full Document Right Arrow Icon
1 Exception Handling Error Handling • Traditional approach: Method returns error code • Problem: Forget to check for error code – Failure notification may go undetected • Problem: Calling method may not be able to do anything about failure – Program must fail too and let its caller worry about it – Many method calls would need to be checked Error Handling • Instead of programming for success – you would always be programming for failure: x.doSomething() if (!x.doSomething()) return false; Throwing Exceptions • Exceptions: – Can't be overlooked – Sent directly to an exception handler–not just caller of failed method • Throw an exception object to signal an exceptional condition • Example: IllegalArgumentException: illegal parameter value IllegalArgumentException exception = new IllegalArgumentException("Amount exceeds balance"); throw exception; Throwing Exceptions • No need to store exception object in a variable: • When an exception is thrown, method terminates immediately – Execution continues with an exception handler throw new IllegalArgumentException("Amount exceeds balance"); Example public class BankAccount { public void withdraw(double amount) { if (amount > balance) { IllegalArgumentException exception = new IllegalArgumentException("Amount exceeds balance"); throw exception; } balance = balance - amount; } . . . }
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Hierarchy of Exception Classes Figure 1: The Hierarchy of Exception Classes Syntax 15.1: Throwing an Exception throw exceptionObject ; Example: throw new IllegalArgumentException(); Purpose: To throw an exception and transfer control to a handler for this exception type Self Check 1. How should you modify the deposit method to ensure that the balance is never negative? 2. Suppose you construct a new bank account object with a zero balance and then call withdraw(10) . What is the value of balance afterwards? Answers 1. Throw an exception if the amount being deposited is less than zero. 2. The balance is still zero because the last statement of the withdraw method was never executed. Checked and Unchecked Exceptions • Two types of exceptions: – Checked • The compiler checks that you don't ignore them • Due to external circumstances that the programmer cannot prevent • Majority occur when dealing with input and output
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/16/2008 for the course CSIS 2102 taught by Professor Teneja during the Spring '07 term at Stockton.

Page1 / 6

exceptions - Error Handling Traditional approach: Method...

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

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