cs110 lecture 20 tuesday, april 13, 2004

21
Lecture 20 1 CS110 Lecture 20 Tuesday, April 13, 2004 • Announcements – hw9 due Thursday, April 15 – exam Tuesday, April 27 • Agenda – Questions – Error handling (JOI Chapter 7 finally!)

Upload: alagan

Post on 03-Feb-2016

59 views

Category:

Documents


0 download

DESCRIPTION

CS110 Lecture 20 Tuesday, April 13, 2004. Announcements hw9 due Thursday, April 15 exam Tuesday, April 27 Agenda Questions Error handling (JOI Chapter 7 finally!). Error handling. Write code so that running program can recognize when an error has occurred tell someone - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS110 Lecture 20 Tuesday, April 13, 2004

Lecture 20 1

CS110 Lecture 20Tuesday, April 13, 2004

• Announcements– hw9 due Thursday, April 15– exam Tuesday, April 27

• Agenda– Questions– Error handling (JOI Chapter 7 finally!)

Page 2: CS110 Lecture 20 Tuesday, April 13, 2004

Lecture 20 2

Error handlingWrite code so that running program can

– recognize when an error has occurred– tell someone– do something sensible

public void withdraw( int amount ){ if ( amount <= balance ) { incrementBalance( - amount );

} else { // an error; what now … ? }}

Page 3: CS110 Lecture 20 Tuesday, April 13, 2004

Lecture 20 3

Help! What next … ?

• First idea: print a messagepublic void withdraw( int amount )

{ if ( amount <= balance ) { incrementBalance( - amount );

} else { System.out.println(“sorry”); }}

Page 4: CS110 Lecture 20 Tuesday, April 13, 2004

Lecture 20 4

To print or not to print?

• Good first idea – but …– in banking system, i/o uses the ATM, but here atm.println( ) fails because BankAccount object doesn’t know about ATM (and shouldn’t)

• In general, printing is a bad idea

• Better idea: tell someone, not necessarily the user

Page 5: CS110 Lecture 20 Tuesday, April 13, 2004

Lecture 20 5

Tell your client: return a valuepublic boolean withdraw( int amount ){ if ( amount <= balance ) { incrementBalance( - amount ); return true;

} return false;}

• Client tests:if (! account.withdraw(amount) ) // print if appropriate

// else return failure to your client

• Client knows that something happened, but not what happened

Page 6: CS110 Lecture 20 Tuesday, April 13, 2004

Lecture 20 6

Use better return value public String withdraw( int amount ){

if ( amount <= balance ){ incrementBalance( - amount ); return “” // or null

} return “insufficient funds”; }

• Client tests return value, has more information– can print, or parse, or return something to whoever

called it

Page 7: CS110 Lecture 20 Tuesday, April 13, 2004

Lecture 20 7

Return an int• Some designs/languages use int return code • C and Unix shell convention:

– return 0 when all goes well– return some n > 0 to report an error

• Design encourages a message catalog:– look up message String using return code as a key (catalog

might be an ArrayList)– provide a catalog in user’s native language, for

internationalization (I18N)• You could invent an Object to return

Page 8: CS110 Lecture 20 Tuesday, April 13, 2004

Lecture 20 8

Two System tricks• If you must print, use System.err rather than System.out

• System.err goes to screen even when System.out is redirected with >

• Terminal class implements errPrintln as well as println

• If you must shut down immediately: if (trueDisaster) System.exit(911);

operating system can check return code from the JVM

Page 9: CS110 Lecture 20 Tuesday, April 13, 2004

Lecture 20 9

When you detect an error, consider

• printing something

• returning something the client can examine

• setting the value of some global (class) variable that a client can look at

• shutting the program down

• throwing an Exception a client can catch

Page 10: CS110 Lecture 20 Tuesday, April 13, 2004

Lecture 20 10

Error handling is painful• Imagining all possible errors is tricky

– you routinely miss some until code breaks– that’s why you have to test!

• Deciding what to do is often difficult (particularly if original design did not plan for handling errors)

• Error handling code – spreads out over the whole program – can make clean logic complicated and ugly– often accounts for 1/4 to 1/3 of real software

Page 11: CS110 Lecture 20 Tuesday, April 13, 2004

Lecture 20 11

Exceptions• Java’s object oriented mechanism for error handling• more powerful, more flexible than using return • Java keywords try, catch, throw• in class: banking system, JOI with Exceptions• for hw: improve Exception handling in Juno 7

• Model– in client: instead of testing a returned value

•try, hoping for the best• prepare to pick up the pieces if necessary (catch)

– in code where the error may occur:• detect error• create a new Exception and throw it

Page 12: CS110 Lecture 20 Tuesday, April 13, 2004

Lecture 20 12

Exceptions (client side)• In client code (for example, Bank.java)

try { account.withdraw( amount ));} catch ( InsufficientFundsException e ) { // look at e, take appropriate action}// processing continues

• If all goes well, catch block code never executes• If withdraw method in BankAccount has a problem it

creates a new InsufficientFundsException object and throws it, to be caught here

Page 13: CS110 Lecture 20 Tuesday, April 13, 2004

Lecture 20 13

Exceptions (service provider)

• Thrown where error happens (The idea in BankAccount.java, not real code …):public int withdraw( int amount )

{

if ( amount > balance ) {

throw new InsufficientFundsException();

}

incrementBalance( -amount ); return amount;

}

Page 14: CS110 Lecture 20 Tuesday, April 13, 2004

Lecture 20 14

Real Bank example• Suppose a customer tries to withdraw more than is in

her account. • Eventually BankAccount.java line 143 executes: if (newBalance < 0) { throw new InsufficientFundsException …

• Read code backward looking for messages (method invocations) to trace methods that are active at that moment in order to see where that Exception is caught

Page 15: CS110 Lecture 20 Tuesday, April 13, 2004

Lecture 20 15

Who calls whom?

• method class line– incrementBalance BA.java 144– withdraw BA.java 77– processTrans… Bank.java 173– visit Bank.java 96– main Bank.java 450

InsufficientFundsException thrown here (line 144)

caught in catch (line 204) after try (lines 161-203)

Page 16: CS110 Lecture 20 Tuesday, April 13, 2004

Lecture 20 16

Method invocation stack

• At any moment while a program is running you can trace the sequence of active methods from the currently executing statement back to main()

• That sequence is the method invocation stack • It’s called the call stack in C - often in Java too

(because it’s easier to say)• The call stack is dynamic, changing as the program runs

(the program itself is static - fixed at compile time)

Page 17: CS110 Lecture 20 Tuesday, April 13, 2004

Lecture 20 17

Stack• The call stack

– grows each time a message invokes a method– shrinks each time a method returns

• main() is always the first thing pushed on to the stack and the last to pop off: when main is done the program is done

• In CS a stack is a last in first out collection– push adds an item to the stack– pop removes one

• The call stack– push a method when it’s invoked– pop a method when it returns

Page 18: CS110 Lecture 20 Tuesday, April 13, 2004

Lecture 20 18

Exceptions and the stack• When error detected (BA.java line 143):

if (newBalance < 0) throw new InsufficientFundsException …

• Normal flow control stops - JVM looks for the nearest catch, which may be– in the running method– somewhere up the call stack

Page 19: CS110 Lecture 20 Tuesday, April 13, 2004

Lecture 20 19

Going back through the stack

• incrementBalance throws an InsufficientFundsException and does not catch it (no try block here)

• The incrementBalance message was sent from BankAccount withdraw method, which doesn’t catch the Exception either (no try block)

• The withdraw message was sent from Bank processTransactionsForAccount method – inside a try block. So control transfers to the matching catch block, which handles the Exception

Page 20: CS110 Lecture 20 Tuesday, April 13, 2004

Lecture 20 20

Keyword throws – lawyers at workprivate final void incrementBalance( int amount ) throws InsufficientFundsException{ if ( ) throw new InsufficientFundsException();}

• Since incrementBalance might throw an InsufficientFundsException and (if it does) it does not catch it, it must declare its intention to throw it on up the stack to its caller by asserting throws InsufficientFUndsException

• throws means might throw, not does throw

Page 21: CS110 Lecture 20 Tuesday, April 13, 2004

Lecture 20 21

Keyword throws – lawyers at workpublic int withdraw( int amount ) throws InsufficientFundsException{ incrementBalance( -amount ); }

• Since withdraw might see an InsufficientFundsException thrown by incrementBalance it must– catch it // it doesn’t– or declare its intention to throw it on up the stack to its caller

by asserting throws InsufficientFUndsException