Using error codes | Using exceptions |
#define OK 1 | try{ |
| |
int routine4(){ | void routine4(){ |
int return_value; | std:exception e; |
// Do something. If it goes wrong, | // Do something. If it goes wrong, |
// tell routine1 by setting | // tell routine1 by throwing a message |
// return_value accordingly | |
| |
return return_value ; | throw(e); |
} | } |
| |
int routine3(){ | void routine3(){ |
int return_value; | |
return_value=routine4(); | routine4(); |
if (return_value != OK) | |
return return_value; | |
... | ... |
} | } |
| |
int routine2(){ | void routine2(){ |
int return_value; | |
return_value=routine3(); | routine3(); |
if (return_value != OK) | |
return return_value; | |
... | ... |
} | } |
| |
int routine1(){ | void routine1(){ |
int return_value; | |
return_value=routine2(); | routine2(); |
if (return_value != OK) | |
// try to recover | |
} | } |
| } // end of the try |
| |
| catch(std:exception& e){ |
| // try to recover |
| } |