Slide 1 Vitaly Shmatikov CS 345 Exceptions. slide 2 Reading Assignment uMitchell, Chapter 8.2.

Slides:



Advertisements
Similar presentations
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
Advertisements

Programming Languages and Paradigms
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function F can return.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
1 Chapter 17-2 Templates and Exceptions Dale/Weems.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Kernighan/Ritchie: Kelley/Pohl:
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
1 Languages and Compilers (SProg og Oversættere) Sequence control and Subprogram Control.
CSE341: Programming Languages Lecture 6 Tail Recursion, Accumulators, Exceptions Dan Grossman Fall 2011.
Dr. Muhammed Al-Mulhem ICS Chapter 7 Scope, Functions, and Storage Management.
Control in Sequential Languages John Mitchell CS 242.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week.
Chapter 10 Storage Management Implementation details beyond programmer’s control Storage/CPU time trade-off Binding times to storage.
Control in Sequential Languages Today’s Slides are from John Mitchell’s Stanford University CS 232 Course.
Run time vs. Compile time
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
Scope, Function Calls and Storage Management John Mitchell CS
Slide 1 Vitaly Shmatikov CS 345 Scope and Activation Records.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
Chapter 13, Slide 1 Exception Handling Exception handling is a language feature that allows the programmer to handle runtime "exceptional conditions."
Control in Sequential Languages + some discussion of functional programming John Mitchell CS 242 Reading: Chapter 8, Sections 8.1 – 8.3 (only) Chapter.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
Runtime Environments Compiler Construction Chapter 7.
10/16/2015IT 3271 All about binding n Variables are bound (dynamically) to values n values must be stored somewhere in the memory. Memory Locations for.
Control in Sequential Languages Mooly Sagiv Original slides by John Mitchell Reading: Chapter 8, Sections 8.1 – 8.3 (only) Chapter 3, Sections 3.3, 3.4.2,
Slide 1 Vitaly Shmatikov CS 345 Imperative Programming.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Basic Semantics Associating meaning with language entities.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
1 9/6/05CS360 Windows Programming CS360 Windows Programming.
CSC 8505 Compiler Construction Runtime Environments.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to John Mitchell and.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Scope, Function Calls and Storage Management John Mitchell CS Reading: Chapter 7, Concepts in Programming Languages.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Control in Sequential Languages
Review: Chapter 5: Syntax directed translation
Memory Locations For Variables
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Chapter 17 Templates and Exceptions Part 2
Exception Handling In Text: Chapter 14.
FP Foundations, Scheme In Text: Chapter 14.
Throwing and catching exceptions
Closure Representations in Higher-Order Programming Languages
Languages and Compilers (SProg og Oversættere)
Dynamic Memory.
Runtime Environments What is in the memory?.
CSE 341 Lecture 11 b closures; scoping rules
Scope, Function Calls and Storage Management
Lecture 9.
Exception Handling and Event Handling
Presentation transcript:

slide 1 Vitaly Shmatikov CS 345 Exceptions

slide 2 Reading Assignment uMitchell, Chapter 8.2

slide 3 Exceptions: Structured Exit uTerminate 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 uTwo 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 necessarily

slide 4 ML Example exception 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 0

slide 5 C++ Example Matrix invert(Matrix m) { if … throw Determinant; … }; try { … invert(myMatrix); … } catch (Determinant) { … // recover from error }

slide 6 C++ vs ML Exceptions uC++ 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, 3 rd ed. uML exceptions Exceptions are a different kind of entity than types Declare exceptions before use Similar, but ML requires what C++ only recommends

slide 7 ML Exceptions uDeclaration: exception  name  of  type  Gives name of exception and type of data passed when this exception is raised uRaise: raise  name   parameters  uHandler:  exp1  handle  pattern  =>  exp2  Evaluate first expression If exception that matches pattern is raised, then evaluate second expression instead General form allows multiple patterns

slide 8 Dynamic Scoping of Handlers exception 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 uDynamic scoping of handlers: in case of exception, jump to most recently established handler on run-time stack uDynamic scoping is not an accident User knows how to handle error Author of library function does not

slide 9 Exceptions 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 10 Exceptions for Efficiency uFunction to multiply values of tree leaves fun prod(LF x) = x | prod(ND(x,y)) = prod(x) * prod(y); uOptimize using exception fun 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) in p(tree) handle Zero=>0 end;

slide 11 Scope of Exception Handlers exception X; (let fun f(y) = raise X and g(h) = h(1) handle X => 2 in g(f) handle X => 4 end) handle X => 6; scope handler Which handler is used?

slide 12 exception X; fun f(y) = raise X fun g(h) = h(1) handle X => 2 g(f) handle X => 4 Dynamic Scope of Handlers (1) formal h handler X2 access link formal y1 access link g(f) f(1) fun f access link fun g Dynamic scope: find first X handler, going up the dynamic call chain leading to “raise X” handler X4 access link

slide 13 Dynamic Scope of Handlers (2) exception X; (let fun f(y) = raise X and g(h) = h(1) handle X => 2 in g(f) handle X => 4 end) handle X => 6; handler X6 formal h handler X2 access link formal y1 access link g(f) f(1) fun f access link fun g Dynamic scope: find first X handler, going up the dynamic call chain leading to “raise X” handler X4 access link

slide 14 Scoping: Exceptions vs. Variables exception X; (let fun f(y) = raise X and g(h) = h(1) handle X => 2 in g(f) handle X => 4 end) handle X => 6; val x=6; (let fun f(y) = x and g(h) = let val x=2 in h(1) in let val x=4 in g(f) end);

slide 15 Static Scope of Declarations val x=6; (let fun f(y) = x and g(h) = let val x=2 in h(1) in let val x=4 in g(f) end); val x6 formal h val x2 access link formal y1 access link g(f) f(1) fun f access link fun g Static scope: find first x, following access links from the reference to X val x4 access link

slide 16 Typing of Exceptions uTyping 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 uTyping of handle  exception  =>  value  Converts exception to normal termination Need type agreement Examples –1 + ((raise X) handle X => e) Type of e must be int (why?) –1 + (e 1 handle X => e 2 ) Type of e 1,e 2 must be int (why?)

slide 17 Exceptions and Resource Allocation exception X; (let val x = ref [1,2,3] in let val y = ref [4,5,6] in … raise X end end); handle X =>... u Resources may be allocated between handler and raise Memory, locks on database, threads … u May be “garbage” after exception General problem, no obvious solution