Unformatted text preview:

CS 345 slide 337 Spring 200526 April 2005Client code can “catch” a thrown exception:void crash( Array& x ){ x[ x.getSize()+2 ]; // a deliberate range error}void f( Array& v ){ // ... try { Array u; ... g(v) ... crash(u) ... h(v) ... }catch( Array::Range ) { // This block catches objects of type Range that // are thrown while preceding try block is active. // That is, it “handles” Array::Range // exceptions. // If Array::operator[] is called with // an invalid index while the try block is active, // control passes directly to this block. // Otherwise this block is skipped. }}CS 345 slide 338 Spring 200526 April 2005When an exception is thrown,• the call stack is unwound to the activation recordof the catching function• intervening functions are forcibly shut down• intervening functions’ local variables aredeallocated —and their destructors, if any, arecalled.The first valid handler encountered by an exceptioncatches it and ends the process.Hencevoid g( ){ try { Array z; f( z ); // catches Array::Range } catch ( Array::Range ) { /* can never be executed */ }}If a program throws an exception for which nohandler is waiting, the program terminates.CS 345 slide 339 Spring 200526 April 2005Different kinds of errors can throw differentexceptions:class Array{public: class Range { }; // range exception class Size { }; // size exception Array( int s = ArraySize ); // ... int& operator[]( int ); int getSize();protected: int *data; int size;};inline int& Array::operator[]( int index ){ if( 0 <= index && index < size ) return data[ index ]; throw Range(); // constructs and throws}Array::Array( int s ){ if( s < 0 || 32767 < s ) throw Size(); // ...}CS 345 slide 340 Spring 200526 April 2005Different exceptions can be handled differently:void f( Array& v ){ // ... try { Array u; ... g(v) ... crash(u) ... } catch( Array::Range ) { // This block handles Array::Range // exceptions. } catch( Array::Size ) { // This block handles Array::Size // exceptions. }}CS 345 slide 341 Spring 200526 April 2005An exception is an object, and can carry informationfrom throw to catch:class Array{public: class Range // exception class { public: int index; Range( int i ){ index = i; } }; Array( int sz = ArraySize ); // ... int& operator[]( int ); int getSize();protected: int *data; int size;};inline int& Array::operator[]( int index ){ if( 0<=index && index<size ) return data[ index ]; throw Range( index );}CS 345 slide 342 Spring 200526 April 2005Extracting an exception object’s information:void f( Array& v ){ // ... try { Array u; ... g(v) ... crash(u) ... } catch( Array::Range r ) { cerr << "bad index " << r.index << '\n'; // ... }}“Catch-all” exception handlers are also allowed:void m(){ try { // possible throw in here } catch( ... ) // catches all exceptions { // do some tidying up… throw; // …and pass the buck }}CS 345 slide 343 Spring 200526 April 2005When an exception is thrown, the stack “unwinds”:void h(){ ...throw bomb();...}void g(){ ...h();...}void f(){ ...g();...}void p(){ ...try{ ... f(); ... }catch( bomb b ){// handle bomb exceptions}}p ptrypftrypfgtrypfghtryp pcatch As each block’s activation record is discarded, all ofits local variables’ destructors are called.CS 345 slide 344 Spring 200526 April 2005How are exception handlers used?A typical example (composite):int f( int arg ){ int success = 0; int result; while( !success ) { try { result = g( arg ); success = 1; } catch( x0 ) { // fix something and try again } catch( x1 ) { // use another method and return a result return 1; } catch( x2 ) { throw; // pass the bug } catch( x3 ) { // transform x3 into some other exception throw xxii; } catch( ... ) { // all other exceptions: give up terminate(); } } return


View Full Document

UT CS 345 - CS 345 Lecture Notes

Download CS 345 Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CS 345 Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CS 345 Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?