DOC PREVIEW
UA CSC 520 - Exceptions

This preview shows page 1-2-16-17-18-34-35 out of 35 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Exception HandlingException Handlingldots Exceptions in PL/ISo, what happens afterwards?Exceptions in PL/IExceptions in PL/Ildots Exceptions in Modula-3Exceptions in Modula-3Exceptions in Modula-3ldots Exceptions in Modula-3ldots Exceptions in Modula-3ldots Exceptions in JavaExceptions as classesException hierarchyCatching an exceptionThe { t finally}-clauseImplementing exceptionsImplementationThe Range TableThe Range Tableldots Unwinding the Stack (Locate)Unwinding the Stack (Locate)ldots Unwinding the Stack (Unwind)Unwinding the Stack (Unwind)ldots The Exception HandlerThe AlgorithmExample --- Explanation of source codeExampleExample --- Explanation of ActionsExceptions in Csetjmp/longjmpsetjmp/longjmpldots Readings and ReferencesSummary520 —Spring 2008 — 30CSc 520Principles of ProgrammingLanguages30 : ExceptionsChristian [email protected] of Computer ScienceUniversity of ArizonaCopyrightc 2008 Christian Collberg[1]520 —Spring 2008 — 30Exception HandlingWhat should a program do if it tries to pop an elementoff an empty stack, or divides by 0, or indexes outsidean array, or produces an arithmetic error, such asoverflow?In C, many procedures will return a status code. In mostcases programmers will “forget” to check this statusflag.Modern languages have built-in exception handlingmechanisms. When an exception is raised (or thrown) itmust be handled or the program will terminate.Exceptions can be raised implicitly by the run-timesystem (overflow, array bounds errors, etc), or explicitlyby the programmer.[2]520 —Spring 2008 — 30Exception Handling...When an exception is raised, the run-time system hasto look for the corresponding handler, the piece of codethat should be executed for the particular exception.The right handler cannot be determined statically (atcompile-time). Rather, we have to do a dynamic(run-time) lookup when the exception is raised.In most languages, you start looking in the current block(or procedure). If it contains no appropriate handler, youreturn from the current routine and re-raise theexception in the caller. This continues until a handler isfound or until we get to the main program (in which casethe program terminates with an error).[3]520 —Spring 2008 — 30Exceptions in PL/I[4]520 —Spring 2008 — 30So, what happens afterwards?What happens after an exception handler has beenfound and executed?resumption model Go back to where the exception wasraised and re-execute the statement (PL/I).termination model Return from the procedure (or unit)containing the handler (Ada).[5]520 —Spring 2008 — 30Exceptions in PL/IExceptions are declared like this:ON conditionstatementThis statement is not actually executed. It’s just“remembered” until later, should an error occur.For example:ON OVERFLOWGOTO error;...error:PRINT "something bad happened"[6]520 —Spring 2008 — 30Exceptions in PL/I...Consider this example:...ON OVERFLOWPRINT "use smaller numbers, please!"...A := A*1000000000000;Where does execution continue after the exceptionhandler has executed? After the location where theexception was thrown.[7]520 —Spring 2008 — 30Exceptions in Modula-3[8]520 —Spring 2008 — 30Exceptions in Modula-3Exceptions are declared like this:INTERFACE M;EXCEPTION Error(TEXT);PROCEDURE P () RAISES {Error};END M;Exceptions can take parameters. In this case, theparameter to Error is a string. Presumably, theprogrammer will return the kind of error in this string.The declaration of P states that it can only raise oneexception, Error.If there is no RAISES clause, the procedure is expectedto raise no exceptions.[9]520 —Spring 2008 — 30Exceptions in Modula-3...S1and S2can raise exceptions implicitly, or theprogrammer can raise an exception explicitly usingRAISE.When the Error-exception is raised, the EXCEPT-blockis searched and the code for the Error exception isexecuted.PROCEDURE P () RAISES {Error};BEGINTRYS1; RAISE Error("Help!"); S2;EXCEPTError (V) => Write(V); |Problem (V) => Write("No Probs!"); |ELSE Write("Unhandled Exception!");END;END P;[10]520 —Spring 2008 — 30Exceptions in Modula-3...An unhandled exception is re-raised in the nextdynamically enclosing TRY-block. If no matchinghandler is found the program is terminated.MODULE M;BEGINTRYTRY S1; EXCEPTProblem (V)=>Write(V);END;EXCEPTError (V) => Write(V); |ELSE Write("Unhandled Exception!");END;END M;[11]520 —Spring 2008 — 30Exceptions in Modula-3...An unhandled exception is re-raised in the callingprocedure. Exception handlers can explicitly re-raise anexception, or raise another exception.MODULE M;PROCEDURE P ();BEGINTRY S1; EXCEPTProblem (V)=>RAISE Error("OK")END;END P;BEGINTRY P(); EXCEPTError (V) => Write(V); |Problem (V) => Write(V);END;END M;[12]520 —Spring 2008 — 30Exceptions in Java[13]520 —Spring 2008 — 30Exceptions as classesIn Java, exceptions are classes. They are declared likethis:class StuipdError extends Exception {}An exception is thrown by creating a new exceptionobject, and then calling throw:throw new StupidError("Forgot to buy milk!")[14]520 —Spring 2008 — 30Exception hierarchyJava defines a hierarchy of exceptions. Programmerscan construct their own by extending one of theseclasses:class Throwable {}class Exception extends Throwable {}class InterruptedException extends Exception {}class RuntimeException extends Exception {}class ArithmeticException extends RuntimeExceptionclass NullPointerException extends RuntimeExceptionclass ClassCastException extends RuntimeExceptionclass Error extends Throwable {}class ThreadDeath extends Error {}[15]520 —Spring 2008 — 30Catching an exceptionExceptions are caught like this:try {throw new Exception("They were out of milk!")} catch (StupidError e) {...} catch (Exception e) {...} catch (Throwable e) {...}Each catch-clause is considered in turn until one isfound that is a subclass of the exception object thrown.[16]520 —Spring 2008 — 30The finally-clauseThe finally-clause is executed regardless of whetheran exception is thrown or not. This can be used to closefiles, etc:try {throw new Exception("They were out of milk!")} catch (StupidError e) {getACow();} finally {eatCornFlakes();}[17]520 —Spring 2008 — 30Implementing exceptions[18]520 —Spring 2008 — 30ImplementationWe want 0-overhead exception handling. This meansthat – unless an exception is raised – there should beno cost associated with the exception handlingmechanism.We allow raising and handling an exception to be


View Full Document

UA CSC 520 - Exceptions

Documents in this Course
Handout

Handout

13 pages

Semantics

Semantics

15 pages

Haskell

Haskell

15 pages

Recursion

Recursion

18 pages

Semantics

Semantics

12 pages

Scheme

Scheme

32 pages

Syllabus

Syllabus

40 pages

Haskell

Haskell

17 pages

Scheme

Scheme

27 pages

Scheme

Scheme

9 pages

TypeS

TypeS

13 pages

Scheme

Scheme

27 pages

Syllabus

Syllabus

10 pages

Types

Types

16 pages

FORTRAN

FORTRAN

10 pages

Load more
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?