Unformatted text preview:

Copyright 1999CS655Paul F. Reynolds, Jr.Exception HandlingCopyright 1999CS655Paul F. Reynolds, Jr.Exception Handling Considerations•What constitutes an exception?–Domain, Range errors–Cristian:Omission: output not thereTiming: Too early / too lateResponse: wrong outputCrash: no response•When an exception is raised, how far can it propagate?•What chain does an exception follow?–static–dynamicCopyright 1999CS655Paul F. Reynolds, Jr.Exception Handling Considerations•What to do with exception raiser•Resume for certain NOTIFY•Resume at handler’s option SIGNAL•Terminate ESCAPE•Should exceptions have parameters?•Should passed over routines be allowed to clean-up?•How should exceptions be handled in parallelenvironment?Copyright 1999CS655Paul F. Reynolds, Jr.CLU & Exception Handling•What is an exception?User defined / failure•Propagation?To caller only (except failure)•Path searched for handler?Dynamic chain•What to do with raiser?Terminate•Parameters?Yes•Declare exceptions thrown?Yes•Clean-up?N/A (no by default)•How handled in parallel?N/ACopyright 1999CS655Paul F. Reynolds, Jr.Ada & Exception Handling•What is an exception?User defined / 4.5 system defined•Propagation?To caller•Path searched for handler?Dynamic chain•What to do with raiser?Terminate•Parameters?No•Declare exceptions thrown?No•Clean-up?N/A (no by default)•How handled in parallelNot propagated environment?Copyright 1999CS655Paul F. Reynolds, Jr.Yemini & Berry: ExceptionHandling•What is an exception?User defined•Propagation?To caller•Path searched for handler?Dynamic chain•What to do with raiser?User choice (replacement model)•Parameters?Yes•Declare exceptions thrown?Yes•Clean-up?Yes•How handled in parallel environment?N/ACopyright 1999CS655Paul F. Reynolds, Jr.C++ & Exception Handling•What is an exception?User defined•Propagation?Thrown to catcher•Path searched for handler?Dynamic chain•What to do with raiser?Terminate•Parameters?Yes•Declare exceptions thrown?No•Clean-up?Yes•How handled in parallel environment?N/ACopyright 1999CS655Paul F. Reynolds, Jr.Java & Exception Handling•What is an exception?User defined•Propagation?Thrown to catcher•Path searched for handler?Dynamic chain•What to do with raiser?Terminate•Parameters?Yes•Declare exceptions thrown?Yes•Clean-up?Yes•How handled in parallel environment?N/ACopyright 1999CS655Paul F. Reynolds, Jr.Raising Exceptions: CLU and AdaExplicit Raising:•CLU: signalIF x < 0 THEN SIGNAL neg(X)•Ada: raiseIF x < 0 THEN RAISE negImplicit Raising:•CLU: systems failures, failure to catch•Ada: four (Ada95; five in Ada83) predefined failures–tasking, program, storage, constraint, numeric (X’d in Ada95)–Ada supports exception raising during elaborationTermination inboth casesCopyright 1999CS655Paul F. Reynolds, Jr.Raising Exceptions: C++ and JavaExplicit Raising:•Throw / catchImplicit Raising:•C++: runtime_error, range_error, overflow_error,underflow_error, bad_alloc, bad_cast…–All built off base class exception•Java: built-in hierarchy with base throwable:–extensive set of exceptions: AbstractMethodError,InternalError, UnknownError, InterruptedException,EmptyStackException, IOException…•floats don’t throw exceptions!Copyright 1999CS655Paul F. Reynolds, Jr.Sample CLU Functionsign = proc(x:int) returns (int) signals (zero, neg (int) )if x < 0 then signal neg(x) elseif x = 0 then signal zero else return (x)endend signNote Java-like requirementto name exceptions that canbe thrown. Of course, CLUhad the requirement first.Copyright 1999CS655Paul F. Reynolds, Jr.Sample Ada FunctionPackage STACK is ERROR: exception; procedure push(x: integer); function pop return integer;end STACK;package body STACK is …. function POP return integer is begin if top = 0 then raise ERROR end if; top := top - 1; return s(top + 1); end POPend STACK;Copyright 1999CS655Paul F. Reynolds, Jr.Handling Exceptions: CLU•CLU: statement levela:= sign(x) EXCEPT WHEN neg(i: int): // handlea:= sign(x) + sign(y) EXCEPT WHEN neg(i: int): // Who raised?BEGINs1; EXCEPT WHEN ... EXIT done (...)s2; ...END EXCEPT WHEN excp1(...) H1; done (...) H2;* Can raise another exception in same procedure using Exit *Copyright 1999CS655Paul F. Reynolds, Jr.Handling Exceptions: Ada•Ada: (frame level)BEGINs1;s2; EXCEPTIONWHEN EXCP1 => H1 {raise}WHEN EXCP2 => H2END•...elaboration level:DECLARE<elaborated stuff> // exceptions handled by invokerBEGINs1; ... // exceptions handled in frame EXCEPTIONWHEN EXCP1 => H1ENDCopyright 1999CS655Paul F. Reynolds, Jr.Propagating Exceptions: CLU and AdaCLU:–to invoker through explicit means–Except for failure exception, propagation is explicit only.–Procedure specifications include ID’s of signalledexceptions•coupling between called procedures and all potential callers?Ada:–to end of frame (statements)–to invoker (elaborations)–Exceptions propagate up dynamic call chain (by default)until caught•interesting interaction with static scopingname passed out of scope and back in againCopyright 1999CS655Paul F. Reynolds, Jr.CLU Failure Initiationnonzero = proc(x: int) returns(int) return (sign(x)) except when neg(y:int): return(y) endend nonzero“zero” exception goes uncaughtFailure(“unhandled exception: zero”) gets propagatedCopyright 1999CS655Paul F. Reynolds, Jr.package D isprocedure A;procedure B; end;procedure OUTSIDE is beginD.A; end OUTSIDE;package body D isERROR: exception;procedure A is begin ... raise ERROR ... end A;procedure B is*Call to D.B can create interesting beginsituation* OUTSIDE; exceptionwhen ERROR => . . .end B; end D;Ada PropagationCopyright 1999CS655Paul F. Reynolds, Jr.CLU’s Failure Exceptions•Only automatically propagated exception is failure–raised if no handler for raised, named exception–can be explicitly raised–occurs if unanticipated failure occurs... thoughts about overloading of "failure"?... thoughts about propagating failure rather than name(Ada)Copyright 1999CS655Paul F. Reynolds, Jr.ParametersCLU: Yes–See Yemini and Berry for argument in favorAda: No ( not even Ada95)–result is Ada can require access to non-locals tostraighten things out–could lead to erroneous programming (determiningparameter passing mechanism)•C++: Yes•Java: YesCopyright 1999CS655Paul F.


View Full Document

UVA CS 655 - Exception Handling

Download Exception Handling
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 Exception Handling 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 Exception Handling 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?