Unformatted text preview:

ExceptionsReading AssignmentExceptions: Structured Exit ML ExampleC++ ExampleC++ vs ML ExceptionsML ExceptionsDynamic Scoping of HandlersExceptions for Error ConditionsExceptions for EfficiencyScope of Exception HandlersDynamic Scope of Handlers (1)Dynamic Scope of Handlers (2)Scoping: Exceptions vs. VariablesStatic Scope of DeclarationsTyping of ExceptionsExceptions and Resource Allocationslide 1Vitaly ShmatikovCS 345Exceptionsslide 2Reading AssignmentMitchell, Chapter 8.2slide 3Exceptions: Structured Exit Terminate part of computation • Jump out of construct• Pass data as part of jump• Return to most recent site set up to handle exception• Unnecessary activation records may be deallocated– May need to free heap space, other resourcesTwo main language constructs• Declaration to establish exception handler• Statement or expression to raise or throw exception Often used for unusual or exceptional condition, but not necessarilyslide 4ML Exampleexception Determinant; (* declare exception name *)fun invert (M) = (* function to invert matrix *)…if …then raise Determinant (* exit if Det=0 *)else …end;...invert (myMatrix) handle Determinant => … ;Value for expression if determinant of myMatrix is 0slide 5C++ ExampleMatrix invert(Matrix m) { if … throw Determinant;…};try { … invert(myMatrix); …}catch (Determinant) { …// recover from error}slide 6C++ vs ML ExceptionsC++ exceptions• Can throw any type• Stroustrup: “I prefer to define types with no other purpose than exception handling. This minimizes confusion about their purpose. In particular, I never use a built-in type, such as int, as an exception.” -- The C++ Programming Language, 3rded.ML exceptions• Exceptions are a different kind of entity than types• Declare exceptions before useSimilar, but ML requires what C++ only recommendsslide 7ML ExceptionsDeclaration: exception 〈name〉 of 〈type〉• Gives name of exception and type of data passed when this exception is raisedRaise: raise 〈name〉〈parameters〉Handler: 〈exp1〉 handle 〈pattern〉 => 〈exp2〉• Evaluate first expression• If exception that matches pattern is raised, then evaluate second expression insteadGeneral form allows multiple patternsslide 8Dynamic Scoping of Handlersexception Ovflw;fun reciprocal(x) = if x<min then raise Ovflw else 1/x;(reciprocal(x) handle Ovflw=>0) / (reciprocal(y) handle Ovflw=>1);–First call to reciprocal() handles exception one way, second call handles it another wayDynamic scoping of handlers: in case of exception, jump to most recently established handler on run-time stackDynamic scoping is not an accident• User knows how to handle error• Author of library function does notslide 9Exceptions for Error Conditions- datatype ‘a tree = LF of ‘a | ND of (‘a tree)*(‘a tree)- exception No_Subtree;- fun lsub (LF x) = raise No_Subtree| lsub (ND(x,y)) = x;> val lsub = fn : ‘a tree -> ‘a tree• This function raises an exception when there is no reasonable value to return– What is its type?slide 10Exceptions for EfficiencyFunction to multiply values of tree leavesfun prod(LF x) = x| prod(ND(x,y)) = prod(x) * prod(y);Optimize using exceptionfun prod(tree) = let exception Zero fun p(LF x) = if x=0 then (raise Zero) else x | p(ND(x,y)) = p(x) * p(y)inp(tree) handle Zero=>0end;slide 11Scope of Exception Handlersexception X;(let fun f(y) = raise Xand g(h) = h(1) handle X => 2ing(f) handle X => 4end) handle X => 6;scopehandlerWhich handler is used?slide 12exception X;fun f(y) = raise Xfun g(h) = h(1) handle X => 2g(f) handle X => 4Dynamic Scope of Handlers (1)formal hhandler X 2access link formal y 1access link g(f)f(1)fun f access link access link fun g Dynamic scope:find first X handler, going up the dynamic call chain leading to “raise X”handler X 4access linkslide 13Dynamic Scope of Handlers (2)exception X;(let fun f(y) = raise Xand g(h) = h(1) handle X => 2ing(f) handle X => 4end) handle X => 6;handler X 6formal hhandler X 2access link formal y 1access link g(f)f(1)fun f access link access link fun g Dynamic scope: find first X handler, going up the dynamic call chain leading to “raise X”handler X 4access linkslide 14Scoping: Exceptions vs. Variablesexception X;(let fun f(y) = raise Xand g(h) = h(1) handle X => 2ing(f) handle X => 4end) handle X => 6;val x=6;(let fun f(y) = xand g(h) = let val x=2 inh(1) inlet val x=4 in g(f) end);slide 15Static Scope of Declarationsval x=6;(let fun f(y) = xand g(h) = let val x=2 inh(1) inlet val x=4 in g(f) end);val x 6formal hval x 2access link formal y 1access link g(f)f(1)fun f access link access link fun g Static scope: find first x, following access links from the reference to Xval x 4access linkslide 16Typing of ExceptionsTyping of raise 〈exn〉• Definition of typing: expression e has type t if normal termination of e produces value of type t• Raising an exception is not normal termination– Example: 1 + raise XTyping of handle 〈exception〉 => 〈value〉• Converts exception to normal termination• Need type agreement• Examples– 1 + ((raise X) handle X => e) Type of emust be int (why?)– 1 + (e1handle X => e2) Type of e1,e2 must be int (why?)slide 17Exceptions and Resource Allocationexception X;(let val x = ref [1,2,3]in let val y = ref [4,5,6]in … raise Xendend); handle X => ...Resources may be allocated between handler and raise• Memory, locks on database, threads …May be “garbage” after exceptionGeneral problem, no obvious


View Full Document

UT CS 345 - Exceptions

Download Exceptions
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 Exceptions 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 Exceptions 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?