CS 345. Exceptions. Vitaly Shmatikov. slide 1

Similar documents
Control in Sequential Languages

Scope, Functions, and Storage Management

Announcements. Scope, Function Calls and Storage Management. Block Structured Languages. Topics. Examples. Simplified Machine Model.

Iterators. Lecture 24: Iterators & Exceptions. Implementing Iterators. When Good Programs Go Bad! - where! Abstract over control structures (in Clu)!

INF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION. Instructors: James Jones Copyright Instructors.

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

DO NOT OPEN UNTIL INSTRUCTED

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Attributes of Variable. Lecture 13: Run-Time Storage Management. Lifetime. Value & Location

References and Exceptions. CS 565 Lecture 14 4/1/08

Standard ML. Exceptions

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Implementing Subprograms

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

What goes inside when you declare a variable?

Homework 5. Notes. Turn-In Instructions. Reading. Problems. Handout 19 CSCI 334: Spring (Required) Read Mitchell, Chapters 6 and 7.

Dynamic Types, Concurrency, Type and effect system Section and Practice Problems Apr 24 27, 2018

More Examples. Lecture 24: More Scala. Higher-Order Functions. Control Structures

Control in Sequential Languages

Topic IV. Block-structured procedural languages Algol and Pascal. References:

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides.

Polymorphism and Type Inference

CS 345. Functions. Vitaly Shmatikov. slide 1

Written by John Bell for CS 342, Spring 2018

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

UNIT 3

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Chapter 9 :: Subroutines and Control Abstraction

Concepts Introduced in Chapter 7

COP4020 Fall 2006 Final Exam

Contents. 8-1 Copyright (c) N. Afshartous

CA31-1K DIS. Pointers. TA: You Lu

Polymorphism and Type Inference

References: Chapters 10 and 11 Chapters 8, and 12( 2 and 3)

Exceptions and Design

Control Abstraction. Hwansoo Han

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

COMPUTER SCIENCE TRIPOS

Overview. finally and resource cleanup

Run-Time Environements

CSE 3302 Programming Languages Lecture 5: Control

Types and Type Inference

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion. Zach Tatlock Winter 2018

Introduction to ML. Based on materials by Vitaly Shmatikov. General-purpose, non-c-like, non-oo language. Related languages: Haskell, Ocaml, F#,

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

Lecture 9: Control Flow

Threads and Continuations COS 320, David Walker

Types for References, Exceptions and Continuations. Review of Subtyping. Γ e:τ τ <:σ Γ e:σ. Annoucements. How s the midterm going?

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

Implementing Subprograms

CSE3322 Programming Languages and Implementation

G Programming Languages - Fall 2012

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CS 3 Introduction to Software Engineering. 3: Exceptions

Variables and Bindings

Continuations and Continuation-Passing Style

Procedure and Object- Oriented Abstraction

Sample Exam; Solutions

Chapter 5 Names, Binding, Type Checking and Scopes

Qualifying Exam in Programming Languages and Compilers

Procedures and Run-Time Storage

CS 314 Principles of Programming Languages. Lecture 13

Compilers and computer architecture: A realistic compiler to MIPS

14. Exception Handling

Compiler Construction

Concurrent Programming using Threads

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

Exceptions. CS162: Introduction to Computer Science II. Exceptions. Exceptions. Exceptions. Exceptions. Exceptions

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

ECE 2035 Programming Hw/Sw Systems Fall problems, 10 pages Final Exam 9 December 2013

Runtime Environments I. Basilio B. Fraguela

The type checker will complain that the two branches have different types, one is string and the other is int

Topic III. LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

INF 212 ANALYSIS OF PROG. LANGS ELEMENTS OF IMPERATIVE PROGRAMMING STYLE. Instructors: Crista Lopes Copyright Instructors.

An Introduction to C++

Lecture 4: MIPS Instruction Set

Programming in Standard ML: Continued

Procedures and Run-Time Storage

Introduction to Java. Handout-3a. cs402 - Spring

238P Operating Systems, Fall Counting Semaphores. Discussed on whiteboard 30 November Slides Posted 10 December 2018

COMP6771 Advanced C++ Programming

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

CS 314 Principles of Programming Languages

CS558 Programming Languages

About this exam review

Assertions and Exceptions

Programming Languages and Compilers (CS 421)

Topic 7: Activation Records

Run-time Environments

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

Run-time Environments

n What is its running time? 9/18/17 2 n poor_rev [1,2,3] = n (poor_rev [1] = n ((poor_rev [1] =

Processes. Johan Montelius KTH

Category Item Abstract Concrete

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

Transcription:

CS 345 Exceptions Vitaly Shmatikov slide 1

Reading Assignment Mitchell, Chapter 8.2 slide 2

Exceptions: 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 necessarily slide 3

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 4

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

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

ML 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 instead General form allows multiple patterns slide 7

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 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 not slide 8

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 9

Exceptions for Efficiency Function to multiply values of tree leaves fun prod(lf x) = x prod(nd(x,y)) = prod(x) * prod(y); Optimize 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 10

Scope of Exception Handlers scope 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 Which handler is used? slide 11

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

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; Dynamic scope: find first X handler, going up the dynamic call chain leading to raise X g(f) f(1) handler X 6 fun f fun g handler X 4 formal h handler X 2 formal y 1 slide 13

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 14

Static Scope of Declarations val 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) fun f fun g end); val x 4 Static scope: g(f) find first x, following access links from the reference to X f(1) formal h val x 2 formal y 1 slide 15

Typing 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 e must be int (why?) 1 + (e 1 handle X => e 2 ) Type of e 1,e 2 must be int (why?) slide 16

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 =>... Resources may be allocated between handler and raise Memory, locks on database, threads May be garbage after exception General problem, no obvious solution slide 17