Lecture #27: ExceptionsApproach I: Do NothingApproach II: Non-Standard ReturnApproach III: Stack manipulationApproach III: DiscussionApproach IV: PC tablesLecture #27: Exceptions• Exception-handling in programmin g languages is a very limi ted for mof continuation.• Execution continues after a function call that is still active whenexception raised.• Java provides mechanism to return a value with the exception , butthis adds no new complexity.Last modified: Wed Mar 30 11:00:25 2005 CS164: Lecture #27 1Approach I: Do Nothing• Some say keep it simple; don’t bother with excepti ons.• Use return code convention:– Example: C library functions often re turn either 0 for O K or non-zero for various degrees of badness.• Problems:Last modified: Wed Mar 30 11:00:25 2005 CS164: Lecture #27 2Approach I: Do Nothing• Some say keep it simple; don’t bother with excepti ons.• Use return code convention:– Example: C library functions often return either 0 for OK or non-zero for various degrees of badness.• Problems:– Forgetting to check.– Code clutter.– Clumsiness: makes value-return i ng fun cti ons less useful.– Slight cost in always checking return codes.Last modified: Wed Mar 30 11:00:25 2005 CS164: Lecture #27 2Approach II: Non-Standard Return• First idea is to modify calls so that they look like this:call _fjmp OKcode to handl e except ionOK:code for norm al re turn• To throw exception:– Put type of exception in some standard reg i ster or memory loca-tion.– Return to instructionafternormal return.• Awkward for the ia32 (a bove). Ea sier on machines that allow retur n-ing to a register+constant offset address [why?].• Exception-handling code decides wh ether it ca n handle the excep-tion, and does another exception return if not.• Problem: Requires small distributed overhead for every functioncall.Last modified: Wed Mar 30 11:00:25 2005 CS164: Lecture #27 3Approach III: Stack manipulation• C does not have a n exception mechanism bui l t into its syntax , butuses library routines:jmp_buf catch_point;void Caller () {if (setjm p (catch_ point) == 0) {normal case, which eventuallygets down to Callee} else {handle e xception}}void Callee () {...// Throw ex c e p tion:longjmp (catch_point, 42);...}...Caller’sframe...otherframes...Callee’sframeCaller’sFP, SP,addr ofsetjmp call& otherscatch point:Last modified: Wed Mar 30 11:00:25 2005 CS164: Lecture #27 4Approach III: Stack manipulation• C does not have a n exception mechanism bui l t into its syntax , butuses library routines:jmp_buf catch_point;void Caller () {if (setjm p (catch_ point) == 0) {normal case, which eventuallygets down to Callee} else {handle e xception}}void Callee () {...// Throw ex c e p tion:longjmp (catch_point, 42);...}...Caller’sframeCaller’sFP, SP,addr ofsetjmp call& otherscatch point:When longjmp called, re-store stack as indicated bycatch poin t and return tothe end of the setjmp call.Last modified: Wed Mar 30 11:00:25 2005 CS164: Lecture #27 4Approach III: Discussi on• On exception, call to setjmp appea rs to return twice, with two dif-ferent values.• Does not require help from compiler,• But implementati on is architecture-specific.• Overhead imposed on every setjmp call .• If used to implement try and catch, therefore, would impos e coston every try.• Subtle problems involving variables that are stored in registers:– The jmp buf typically has to store such registers, but– That means the value of some local variables ma y revert unpre-dictably upon a long jmp.Last modified: Wed Mar 30 11:00:25 2005 CS164: Lecture #27 5Approach IV: PC tables• Sun’s Java implemen ta ti on uses a differ ent a pproach.• Compiler generates a table mapping instruction address es (programcounter (PC) values) to exception handlers for each function.• If needed, compiler also leaves be hind infor ma ti on necessary to re-turn from a function (“unw i nd the stack”) when exception thrown.• To throw exception E:while (curren t PC doesn’t map to handler for E)unwind stack to l a st caller• Under this approach, a try-catch incurs no cost unless there is anexception , but• Throwing and handl ing the ex ce ption more expen sive than other ap-proaches , and• Tables add space.Last modified: Wed Mar 30 11:00:25 2005 CS164: Lecture #27
View Full Document