Exceptions in Programming: Efficient Implementation and Use - Prof. Westley Weimer, Study notes of Programming Languages

The implementation and use of exceptions in programming languages. It covers the language system structure, the challenges of error handling, and the benefits of using exceptions. The document also provides examples of automated grade assignment and the addition of exceptions to the cool language.

Typology: Study notes

Pre 2010

Uploaded on 03/19/2009

koofers-user-4hp
koofers-user-4hp 🇺🇸

9 documents

1 / 53

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
#1
ACM Trivia Bowl
Today
7pm OLS 011
Snacks and drinks provided
All are welcome! I will be there.
email eas2h with team or just show up
If you are in one of the top three teams, I
will give you one point of extra credit (as
if you had found a bug in Cool).
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35

Partial preview of the text

Download Exceptions in Programming: Efficient Implementation and Use - Prof. Westley Weimer and more Study notes Programming Languages in PDF only on Docsity!

ACM Trivia Bowl

Today

• 7pm OLS 011

• Snacks and drinks provided

• All are welcome! I will be there.

– email eas2h with team or just show up

• If you are in one of the top three teams, I

will give you one point of extra credit (as

if you had found a bug in Cool).

“Wizard is about to die.”

  • (^) PA5 is due Wednesday April 23 rd - that's about

three weeks from now.

  • I have only one auto-tester submission as lunch

time today.

  • I am predicting that you haven't started PA5 yet.
  • You will have second midterms in this class

(and others!) between then and now.

  • If you can't interpret hello-world.cl by the end

of this weekend, I forsee regret, remorse and

lack of sleep in your future.

One-Slide Summary

  • Real-world programs must have error-

handling code. Errors can be handled where

they are detected or the error can be

propagated to a caller.

  • Passing special error return codes is itself

error-prone.

  • Exceptions are a formal and automated way

of reporting and handling errors. Exceptions

can be implemented efficiently and described

formally.

Language System Structure

  • We looked at each stage in

turn

  • A new language feature

affects many stages

  • We will add exceptions Source Lexer Parser Code Generator Runtime System Run it! Type checker

Exceptional Motivation

  • “Classroom” programs are written with optimistic assumptions
  • Real-world programs must consider “exceptional” situations: - Resource exhaustion (disk full, out of memory, network packet collision, …) - Invalid input - Errors in the program (null pointer dereference)
  • It is usual for code to contain 1-5% error handling code (figures for modern Java open source code) - (^) With 3-46% of the program text transitively reachable

Why do we care?

  • Are there any implications if software makes

mistakes?

Error Return Codes

  • The callee can signal the error by returning a

special return value or error code :

  • Must not be one of the valid inputs
  • Must be agreed upon beforehand (i.e., in API)
    • What's an example?
  • The caller promises to check the error return

and either:

  • Correct the error, or
  • Pass it on to its own caller

Error Return Codes

  • It is sometimes hard to select return codes
    • What is a good error code for:
      • divide(num: Double, denom: Double) : Double { … }
  • 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 codes

Example: 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); }

Example: 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); } A lot of extra code

Example: 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 type Error codes are sometimes arbitrary A lot of extra code

Exceptions

  • 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 checking

Adding Exceptions to Cool

(Informal) semantics of try e catch x : T ) e

1

  • e is evaluated first
  • 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^ e 1 with^ x^ bound to the exception parameter
  • The (normal or exceptional) result of ev
  • aluating^ e 1 becomes the result of the entire expression Else
  • The entire expression terminates exceptionally

Example:

Automated Grade Assignment

float 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 “Aie!?\n”; } }