DOC PREVIEW
Berkeley COMPSCI 164 - Lecture Notes

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:

Exceptions. Language Design and Implementation IssuesStructure of a CompilerLecture SummaryExceptions. Motivation.Exceptions. MotivationExceptions. Error Return Codes.Error Return CodesExample: Automated Grade AssignmentSlide 9ExceptionsAdding Exceptions to CoolSlide 12Example: Automated Grade AssignmentExample. Notes.OverviewTyping ExceptionsSlide 17Slide 18Slide 19Code Generation for ExceptionsImplementing Exceptions with Long Jumps (1)Long Jumps. Example.Implementing Exceptions with Long Jumps (2)Implementing Exceptions with Long Jumps (3)Long JumpsImplementing Exceptions with Tables (1)Implementing Exceptions with Tables (2)Implementing Exceptions with Tables. Example.Implementing Exceptions with Tables. Notestry … finally …Code Generation for try … finallyAvoiding Code Duplication for try … finallyJVML Subroutines Are ComplicatedAre JVML Subroutines Worth the Trouble ?Exceptions. ConclusionProf. Bodik CS 164 Lecture 22 1Exceptions.Language Design and Implementation IssuesLecture 22Prof. Bodik CS 164 Lecture 22 2Structure of a Compiler•We looked at each stage in turn•A new language feature affects many stages•We will add exceptionsSourceLexerParserCode GeneratorRuntime SystemExecutableType checkerProf. Bodik CS 164 Lecture 22 3Lecture Summary•Why exceptions ?•Syntax and informal semantics•Semantic analysis (i.e. type checking rules)•Code generation•Runtime system supportProf. Bodik CS 164 Lecture 22 4Exceptions. Motivation.•“Classroom” programs are written with optimistic assumptions•Real-world programs must consider “exceptional” situations:–Resource exhaustion (disk full, out of memory, …)–Invalid input–Errors in the program (null pointer dereference)•It is usual for solid code to contain 30-50% error handling code !Prof. Bodik CS 164 Lecture 22 5Exceptions. MotivationTwo ways of dealing with errors:1. Handle them where you detect them•E.g., null pointer dereference ! stop execution2. Let the caller handle the errors:•The caller has more contextual informationE.g. an error when opening a file:a) In the context of opening /etc/passwdb) In the context of opening a log file •But then we must tell the caller about the error !Prof. Bodik CS 164 Lecture 22 6Exceptions. Error Return Codes. •The callee can signal the error by returning a special return value:–Must not be one of the valid return values–Must be agreed upon beforehand•The caller promises to check the error return and either:–Correct the error, or–Pass it on to its own callerProf. Bodik CS 164 Lecture 22 7Error Return Codes•It is sometimes hard to select return codes–What is a good error code for “double divide(…)”?•How many of you always check errors for:–malloc(int) ?–open(char *) ?–close(int) ?–time(struct time_t *) ?•Easy to forget to check error return codesProf. Bodik CS 164 Lecture 22 8Example: Automated Grade Assignment float getGrade(int sid) { return dbget(gradesdb, sid); }void setGrade(int sid, float grade) { dbset(gradesdb, sid, grade); }void extraCredit(int sid) {setGrade(sid, 0.33 + getGrade(sid));}void grade_inflator() {while(gpa() < 3.0) { extraCredit(random()); } }•What errors are we ignoring here ?Prof. Bodik CS 164 Lecture 22 9Example: Automated Grade Assignment float getGrade(int sid) { float res; int err = dbget(gradesdb, sid, &res); if(err < 0) { return -1.0;} return res;}int extraCredit(int sid) { int err; float g = getGrade(sid); if(g < 0.0) { return 1; } err = setGrade(sid, 0.33 + g)); return (err < 0); }Some functions change their typeError codes are sometimes arbitraryA lot of extracodeProf. Bodik CS 164 Lecture 22 10Exceptions•Exceptions are a language mechanism designed to allow: –Deferral of error handling to a caller–Without (explicit) error codes–And without (explicit) error return code checkingProf. Bodik CS 164 Lecture 22 11Adding Exceptions to Cool•We extend the language of expressions: e ::= throw e | try e catch x : T ) e’•(Informal) semantics of throw e–Signals an exception–Interrupts the current evaluation and searches for an exception handler up the activation chain–The value of e is an exception parameter and can be used to communicate details about the exceptionProf. Bodik CS 164 Lecture 22 12Adding Exceptions to Cool(Informal) semantics of try e catch x : T ) e11. e is evaluated first2. If e’s evaluation terminates normally with v then v is the result of the entire expression Else (e’s evaluation terminates exceptionally) If the exception parameter is of type · T then–Evaluate e1 with x bound to the exception parameter–The (normal or exceptional) result of evaluating e1 becomes the result of the entire expression Else–The entire expression terminates exceptionallyProf. Bodik CS 164 Lecture 22 13Example: Automated Grade Assignmentfloat getGrade(int sid) { return dbget(gradesdb, sid); }void setGrade(int sid, float grade) { if(grade < 0.0 || grade > 4.0) { throw (new NaG); } dbset(gradesdb, sid, grade); }void extraCredit(int sid) {setGrade(sid, 0.33 + getGrade(sid)) }void grade_inflator() {while(gpa < 3.0) { try extraCredit(random()) catch x : Object ) print “Nice try! Don’t give up.\n”; } }Prof. Bodik CS 164 Lecture 22 14Example. Notes.•Only error handling code remains•But no error propagation code–The compiler handles the error propagation–No way to forget about it–And also much more efficient (we’ll see)•Two kinds of evaluation outcomes:–Normal return (with a return value)–Exceptional “return” (with an exception parameter)–No way to get confused which is whichProf. Bodik CS 164 Lecture 22 15OverviewWhy exceptions ?Syntax and informal semantics•Semantic analysis (i.e. type checking rules)•Code generation•Runtime system supportProf. Bodik CS 164 Lecture 22 16Typing Exceptions•We must extend the Cool typing judgment O, M, C ` e : T–Type T refers to the normal return !•We’ll start with the rule for try:–Parameter “x” is bound in the catch expression–try is like a conditionalO, M, C ` e : T1 O[T/x], M, C ` e’ : T2O, M , C ` try e catch x : T ) e’ : T1 t T2Prof. Bodik CS 164 Lecture 22 17Typing Exceptions•What is the type of “throw e” ?•The type of an expression: –Is a description of the possible return values, and–Is used to decide in what contexts we can use the expression•“throw” does not return to its


View Full Document

Berkeley COMPSCI 164 - Lecture Notes

Documents in this Course
Lecture 8

Lecture 8

40 pages

Load more
Download Lecture Notes
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 Lecture Notes 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 Lecture Notes 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?