Overview. finally and resource cleanup

Similar documents
Boolean expressions. Elements of Programming Languages. Conditionals. What use is this?

14. Exception Handling

Variables. Substitution

CS558 Programming Languages

Exception Handling: Control. Exception handling is the control of error conditions or other unusual events during the execution of a program.

Overview. Elements of Programming Languages. Evaluation order. Evaluation order

CS 3 Introduction to Software Engineering. 3: Exceptions

Chapter 14. Exception Handling and Event Handling ISBN

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

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

Chapter 14. Exception Handling and Event Handling

Exceptions. Errors and Exceptions. Dealing with exceptions. What to do about errors and exceptions

Formal Methods for Java

CS558 Programming Languages

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

G Programming Languages - Fall 2012

ECE 122. Engineering Problem Solving with Java

Programming Languages and Techniques (CIS120)

CS558 Programming Languages

Object Oriented Programming

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

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 7

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

CSCI-GA Final Exam

CS558 Programming Languages

Preconditions. CMSC 330: Organization of Programming Languages. Signaling Errors. Dealing with Errors

Exceptions Handling Errors using Exceptions

Errors and Exceptions

Exception-Handling Overview

Programming II (CS300)

Chapter 13 Exception Handling

Defensive Programming

Exceptions. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 15

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

Chapter 14. Exception Handling and Event Handling 异常处理和事件处理. 孟小亮 Xiaoliang MENG, 答疑 ISBN

Category Item Abstract Concrete

Sri Vidya College of Engineering & Technology Question Bank

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming.

Object Oriented Programming

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Exception Handling. General idea Checked vs. unchecked exceptions Semantics of... Example from text: DataAnalyzer.

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

JAC444 - Lecture 4. Segment 1 - Exception. Jordan Anastasiade Java Programming Language Course

Relation Overriding. Syntax and Semantics. Simple Semantic Domains. Operational Semantics

Exceptions in Java

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

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

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

Exceptions. CSC207 Winter 2017

Exception handling refactorings. OOA/OOD Fudan University

Overview. Elements of Programming Languages. Another example. Consider the humble identity function

CS 112 Programming 2. Lecture 08. Exception Handling & Text I/O (1) Chapter 12 Exception Handling and Text IO

Today. Elements of Programming Languages. Concrete vs. abstract syntax. L Arith. Lecture 1: Abstract syntax

CS558 Programming Languages

Programming Languages and Techniques (CIS120e)

National University. Faculty of Computer Since and Technology Object Oriented Programming

About This Lecture. Outline. Handling Unusual Situations. Reacting to errors. Exceptions

C16b: Exception Handling

6.Introducing Classes 9. Exceptions

CS558 Programming Languages

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Programming Languages and Techniques (CIS120)

16-Dec-10. Consider the following method:

Java Review: Objects

Input-Output and Exception Handling

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Lecture 14: Exceptions 10:00 AM, Feb 26, 2018

Contents. I Introductory Examples. Topic 06 -Exception Handling

What is the purpose of exceptions and exception handling? Vocabulary: throw/raise and catch/handle Exception propagation Java checked and unchecked

Chapter 12 Exception Handling

Java Loose Ends. 11 December 2017 OSU CSE 1

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

Exceptions - Example. Exceptions - Example

Object Oriented Programming. Week 7 Part 1 Exceptions

CS 11 Ocaml track: lecture 2

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

Exceptions. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

Control in Sequential Languages

PIC 20A Exceptions. Ernest Ryu UCLA Mathematics. Last edited: November 27, 2017

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

Defensive Programming. Ric Glassey

The story so far. Elements of Programming Languages. Pairs in various languages. Pairs

Grade Weights. Language Design and Overview of COOL. CS143 Lecture 2. Programming Language Economics 101. Lecture Outline

Lecture 3: Recursion; Structural Induction

BBM 102 Introduction to Programming II Spring Exceptions

COMP200 EXCEPTIONS. OOP using Java, based on slides by Shayan Javed

CSci 4223 Lecture 12 March 4, 2013 Topics: Lexical scope, dynamic scope, closures

Verifying pattern matching with guards in Scala

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

Programming II (CS300)

Variables and Bindings

Exceptions. Examples of code which shows the syntax and all that

Exceptions. Produced by. Algorithms. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

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

Overview. Elements of Programming Languages. Records. Named variants. Last time: Simple data structures: pairing (product types), choice (sum types)

Transcription:

Overview Elements of Programming Languages Lecture 16: Exceptions and Control Abstractions James Cheney University of Edinburgh November 24, 2015 We have been considering several high-level aspects of language design: Type soundness References Evaluation order Today we complete this tour and examine: Exceptions Tail recursion Other control abstractions Exceptions finally and resource cleanup In earlier lectures, we considered several approaches to error handling Exceptions are another popular approach (supported by Java, C++, Scala, ML, Python, etc.) The throw e statement raises an exception e Atry/catchblockrunsastatement;ifanexceptionis raised, control transfers to the corresponding handler try { do something catch (IOException e) { handle exception e catch (NullPointerException e) { handle another exception What if the try block allocated some resources? We should make sure they get deallocated! finally clause: gets run at the end whether or not exception is thrown InputStream in = null; try { in = new FileInputStream(fname); do something with in catch (IOException exn) { finally { if(in!= null) in.close(); Java 7: try-with-resources encapsulates this pattern, for resources implementing AutoCloseable interface

throws clauses In Java, potentially unhandled exceptions typically need to be declared in the types of methods void writefile(string filename) throws IOException { InputStream in = new FileInputStream(filename); write to file in.close(); This means programmers using such methods know that certain exceptions need to be handled Failure to handle or declare an exception is a type error! (however, certain unchecked exceptions / errors do not need to be declared, e.g. NullPointerException) Exceptions for shortcutting Exceptions in Scala As you might expect, Scala supports a similar mechanism: try { do something catch { case exn: IOException => handle IO exception case exn: NullPointerException => handle null pointer exception finally { cleanup Main di erence: The catch block is just a Scala pattern match on exceptions Scala allows pattern matching on types (via isinstanceof/asinstanceof) Also: throws clauses not required Exceptions in practice We can also use exceptions for normal computation def product(l: List[Int]) = { object Zero extends Throwable def go(l:list[int]): Int = l match { case Nil => 1 case x::xs => if (x == 0) {throw Zero else {x * go(xs) try { go(l) catch { case Zero => 0 potentially saving a lot of e ort if the list contains 0 Java: Exceptions are subclasses of java.lang.throwable Method types must declare (most) possible exceptions in throws clause compile-time error if an exception can be raised and not caught or declared multiple catch blocks; finally clause to allow cleanup Scala: doesn t require declaring thrown exceptions: this becomes especially painful in a higher-order language catch does pattern matching

Modeling exceptions Exceptions and types Exception constructs are straightforward to typecheck: We will formalize a simple model of exceptions: e ::= raise e e 1 handle {x ) e 2 Here, raise e throws an arbitrary value as an exception while e 1 handle {x ) e 2 evaluates e 1 and, if an exception is thrown during evaluation, binds the value v to x and evaluates e. Define L Exn as L Rec extended with exceptions ::= exn Usually, the exn type is extensible (e.g. by subclassing) ` e : for L Exn ` e : exn ` raise e : ` e 1 :, x : exn ` e 2 : ` e 1 handle {x ) e 2 : Note: raise e can have any type! (because raise e never returns) The return types of e 1 and e 2 in handler must match. Interpreting exceptions We can extend our Scala interpreter for L Rec to manage exceptions as follows: case class ExceptionV(v: Value) extends Throwable def eval(e: Expr): Value = e match { case Raise(e: Expr) => throw (ExceptionV(eval(e))) case Handle(e1: Expr, x: Variable, e2:expr) => try { eval(e1) catch (ExceptionV(v)) { eval(subst(e2,v,x)) This might seem a little circular! Semantics of exceptions To formalize the semantics of exceptions, we need an auxiliary judgment e raises v Intuitively: this says that expression e does not finish normally but instead raises exception value v e raises v raise v raises v e 1 e 1 raises v e 2 raises v v 1 e raises v if e then e 1 else e 2 raises v e 1 raises v e 2 raises v The most interesting rule is the first one; the rest are administrative

Semantics of exceptions We can now define the small-step semantics of handle using the following additional rules: e 7! e 0 e 1 7! e 0 1 e 1 handle {x ) e 2 7! e 0 1 handle {x ) e 2 v 1 handle {x ) e 2 7! v 1 e 1 raises v e 1 handle {x ) e 2 7! e 2 [v/x] If e 1 evaluates normally to a value, step to it If e 1 raises an exception v, substituteitinforx and evaluate e 2 Tail recursion Afunctioncallisatail call if it is the last action of the calling function. If every recursive call is a tail call, we say f is tail recursive. For example, this version of fact is not tail recursive: def fact1(n: Int): Int = if (n == 0) {1 else {n * (fact(n-1)) But this one is: def fact2(n: Int) = { def go(n: Int, r: Int): Int = if (n == 0) {r else {go(n-1,n*r)) go(n,1) Tail recursion and e ciency Continuations [non-examinable] Tail recursive functions can be compiled more e ciently because there is no more work to do after the recursive call In Scala, there is a (checked) annotation @tailrec to mark tail-recursive functions for optimization def fact2(n: Int) = { @tailrec def go(n: Int, r: Int): Int = if (n == 0) {r else {go(n-1,n*r) go(n,1) Conditionals, while-loops, exceptions, goto are all form of control abstraction Continuations are a highly general notion of control abstraction, which can be used to implement exceptions (and much else). Material covered from here on is non-examinable. just for fun! (Depends on your definition of fun, I suppose)

Continuations Acontinuationisafunctionrepresenting therestofthe computation Any function can be put in continuation-passing form for example def fact3[a](n: Int, k: Int => A): A = if (n == 0) {k(1) else {fact3(n-1, {m => k (n * m)) This says: if n is 0, pass 1 to k otherwise, recursively call with parameters n r.k(n r) 1and when done, multiply the result by n and pass to k Interpreting L Arith using continuations How does this work? def fact3[a](n: Int, k: Int => A): A = if (n == 0) {k(1) else {fact3(n-1, {r => k (n * r)) fact3(3, x.x) 7! fact3(2, r 1.( x.x) (3 r 1 )) 7! fact3(1, r 2.( r.( x.x) (3 r)) (2 r 2 )) 7! fact3(0, r 3.( r 2.( r 1.( x.x) (3 r 1 )) (2 r 2 )) (1 r 3 )) 7! ( r 3.( r 2.( r 1.( x.x) (3 r 1 )) (2 r 2 )) (1 r 3 )) 1 7! ( r 2.( r 1.( x.x) (3 r 1 )) (2 r 2 )) (1 1) 7! ( r 1.( x.x) (3 r 1 )) (2 1) 7! ( x.x) (3 2) 7! 6 Interpreting L If using continuations // Arithmetic case Num(n) => k(numv(n)) case Plus(e1,e2) => eval(e1,{case NumV(v1) => eval(e2,{case NumV(v2) => k(numv(v1+v2)))) case Times(e1,e2) => eval(e1,{case NumV(v1) => eval(e2,{case NumV(v2) => k(numv(v1*v2)))) // Booleans case Bool(n) => k(boolv(n)) case Eq(e1,e2) => eval(e1,{v1 => eval(e2,{v2 => k(boolv(v1 == v2)))) case IfThenElse(e,e1,e2) => eval(e,{case BoolV(v) => if(v) { eval(e1,k) else { eval(e2,k) )

Interpreting L Let using continuations Interpreting L Rec using continuations // Let-binding case Let(e1,x,e2) => eval(e1,{v => eval(subst(e2,v,x),k)) // Functions case Lambda(x,ty,e) => k(lambdav(x,ty,e)) case Rec(f,x,ty1,ty2,e) => k(recv(f,x,ty1,ty2,e)) case Apply(e1,e2) => eval(e1, {v1 => eval(e2, {v2 => v2 match { case LambdaV(x,ty,e) => eval(subst(e,v2,x), k) case RecV(f,x,ty1,ty2,e) => eval(subst(subst(e,v2,x),v1,f),k) )) Interpreting L Exn using continuations Summary To deal with exceptions, we add a second continuation h for handling exceptions. (Cases seen so far just pass h along.) def eval[a](e: Expr, h: Value => A, k: Value => A): A = e match { // Exceptions case Raise(e0) => eval(e0,h,h) case Handle(e1,x,e2) => eval(e1,{v => eval(subst(e2,v,x),k,h),k) When raising an exception, we forget k and pass to h. When handling, we install new handler using e2 Today we completed our tour of Type soundness References and resource management Evaluation order Exceptions and control abstractions (today) which can interact with each other and other language features in subtle ways Next time: review lecture information about exam, reading