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

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

Control in Sequential Languages

Lecture 9: Control Flow

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

Chapter 7 Control I Expressions and Statements

ECE 122. Engineering Problem Solving with Java

14. Exception Handling

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

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

Control in Sequential Languages

Lecture 19 Programming Exceptions CSE11 Fall 2013

Chapter 14. Exception Handling and Event Handling ISBN

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

Chapter 13 Exception Handling

CS 3 Introduction to Software Engineering. 3: Exceptions

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

CMSC131. Exceptions and Exception Handling. When things go "wrong" in a program, what should happen.

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

C16b: Exception Handling

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

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

Exceptions in Java

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

Overview. finally and resource cleanup

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

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

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

CS 345. Exceptions. Vitaly Shmatikov. slide 1

Exceptions - Example. Exceptions - Example

G Programming Languages - Fall 2012

Chapter 14. Exception Handling and Event Handling

Errors and Exceptions

GRAMMARS & PARSING. Lecture 7 CS2110 Fall 2013

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

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

Object Oriented Programming. Week 7 Part 1 Exceptions

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

Defensive Programming. Ric Glassey

CS558 Programming Languages

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

CSE 331 Software Design & Implementation

UNIT 3

Exceptions and Design

Exception-Handling Overview

Object Oriented Programming

Programming Languages and Techniques (CIS120)

CS112 Lecture: Exceptions. Objectives: 1. Introduce the concepts of program robustness and reliability 2. Introduce exceptions

Exception Handling. Handling bad user input. Dr. Siobhán Drohan Maireád Meagher. Produced by:

COSC 123 Computer Creativity. I/O Streams and Exceptions. Dr. Ramon Lawrence University of British Columbia Okanagan

LECTURE 18. Control Flow

Programming II (CS300)

Fundamentals of Object Oriented Programming

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

CS112 Lecture: Exceptions and Assertions

COP4020 Fall 2006 Final Exam

Exception Handling Introduction. Error-Prevention Tip 13.1 OBJECTIVES

Exceptions and I/O: sections Introductory Programming. Errors in programs. Exceptions

Introductory Programming Exceptions and I/O: sections

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

BBM 102 Introduction to Programming II Spring Exceptions

COMP-202 Unit 9: Exceptions

Exceptions and assertions

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

COMP-202 Unit 9: Exceptions

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

COMP1008 Exceptions. Runtime Error

Programming II (CS300)

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

More on Exception Handling

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

More on Exception Handling

Object Oriented Programming

EXCEPTIONS. Fundamentals of Computer Science I

Object Oriented Programming Exception Handling

Java Errors and Exceptions. Because Murphy s Law never fails

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

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

BBM 102 Introduction to Programming II Spring 2017

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

Correctness and Robustness

Exceptions. CSC207 Winter 2017

Std 12 Lesson-10 Exception Handling in Java ( 1

CS558 Programming Languages

Control Abstraction. Hwansoo Han

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

CMSC 330: Organization of Programming Languages

Sri Vidya College of Engineering & Technology Question Bank

Logic Programming II & Revision

Control Structures. Outline. In Text: Chapter 8. Control structures Selection. Iteration. Gotos Guarded statements. One-way Two-way Multi-way

Internal Classes and Exceptions

Java Loose Ends. 11 December 2017 OSU CSE 1

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Exceptions, Case Study-Exception handling in C++.

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

G Programming Languages - Fall 2012

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

EXCEPTION HANDLING. Summer 2018

According to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe!

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

CS 251 Intermediate Programming Exceptions

Transcription:

Iterators Lecture 24: Iterators & Exceptions CSC 131 Fall, 2014 Kim Bruce Abstract over control structures (in Clu) - where for c : char in string_chars(s) do string_chars = iter (s : string) yields (char); index : Int := 1; limit : Int := string$size (s); while index <= limit do yield (string$fetch(s, index)); index := index + 1; end string_chars; Implementing Iterators Just another object w/state in o-o language What about procedural? How can we retain state? When Good Programs Go Bad Specific kind of coroutine.

Handling Errors Exceptions What happens when something goes wrong, e.g., with read from file. In C returns error condition, which is generally ignored. In more modern languages, throw exception, which must be handled or crash. Designed to handle unexpected errors. Exception handlers based on dynamic calls, not static scope. Allows program to recover from exceptional conditions, esp. beyond programmers control Can be abused Example Exceptions Exception Handling Arithmetic, array bounds, or I/O faults, Failure of preconditions Unpredictable conditions Tracing program flow in debugger Ada: - raise exception_name; - handling: begin C exception when excp_name1 => C' when excp_name2 => C'' when others => C' Java, C++ similar w/ throw & try-catch

Handling Exceptions After Handling... When throw exception -- where look for handler? - Same unit? (Ada/C++/Java) - Calling unit? (Clu) - If not find, continue up call chain (Ada/Java/ML/Haskell): Return from block PL/I: Resumption model: re-execute failed statement. Eiffel: Re-execute block where failure occurred ML & Java -- exceptions can take parameters Haskell uses Monads data Exn a = Oops String Answer a deriving (Show) instance Monad Exn where return a = Answer a -- return :: a -> Exn a -- (>>=) :: M a -> (a -> M b) -> M b (Oops s) >>= f = Oops s (Answer a) >>= f = f a throw :: String -> Exn a throw = Oops catch :: Exn a -> (String -> Exn a) -> Exn a catch (Oops l) h = h l catch (Answer r) _ = Answer r See Stone s ExcInterp.hs for interpreter handling exceptions Exceptions in Java Objects from subclass of Exception class try { Pattern matching } catch (ExcType ex) { } catch (ExcType ex) {...} If not caught, must declare. E.g. public E pop() throws EmptyStackException {... throw new EmptyStackException(); }

RuntimeException If Exception Not Handled If exceptions subclasses of RuntimeException then need not be declared in method headers Ex.: - NullPointerException, ArrayIndexOutOfBoundsException, IllegalArgumentException, NumberFormatException, and ArithmeticException Unfortunately, also includes EmptyStackException Pop off activation records while searching for handler. What if allocated memory in unit being popped? OK if garbage collection, but Closing files also problems Talk later about problems Java try-catch-finally So far... try { } catch (ExcType ex) { } catch (Exc'Type ex) { } finally {... } No matter how you complete block, will execute finally clause Structured Programming - Goto considered harmful Exceptions - Structured jumps -- can carry a value - dynamic scoping of exception handler Continuations...

Continuations Continuation of expression is remaining work to be done after evaluating expression - the future - Represented as a function, applied to value of exp, which is value computed so far. Capture continuation - use it later to return to execution. Explicitly represented in Scheme, ML Have been important in compilers for functional languages, concurrency, web programming Example 0: Roots of Quadratic exception notquadratic; exception imaginaryroots; fun equal(x:real, y:real) = x <= y andalso x >= y; fun roots(a, b, c) = let val discbase = (b*b) - (4.0*a*c) val denom = 2.0*a if equal(denom, 0.0) then raise notquadratic else if discbase < 0.0 then raise imaginaryroots else let val disc = Math.sqrt(discBase) if disc > 0.0 then [(~b + disc)/denom, (~b - disc)/denom] else [~b/denom] Example 1: Roots of Quadratic fun checkquad (x, n_continuation, e_continuation) = if equal(x, 0.0) then e_continuation() else n_continuation(x); fun roots1(a, b, c) = let fun econt () = raise notquadratic fun ncont (x) = let val discbase = (b*b) - (4.0*a*c) if discbase < 0.0 then raise imaginaryroots else let val disc = Math.sqrt(discBase) if disc>0.0 then [(~b + disc)/x, (~b - disc)/x] else [~b/x] checkquad(2.0*a, ncont, econt) Example 2: Roots of Quadratic fun checkimaginary (x, n_continuation, e_continuation) = if x < 0.0 then e_continuation() else n_continuation(math.sqrt(x)); fun roots2(a, b, c) = let fun econt () = raise notquadratic fun ncont (x) = let fun econt () = raise imaginaryroots fun ncont (disc) = if disc > 0.0 then [(~b + disc)/x, (~b - disc)/x] else [~b/x] checkimaginary(b*b - 4.0*a*c, ncont, econt) checkquad(2.0*a, ncont, econt)

Example 3: Roots of Quadratic fun checknumroots(disc, continuation1, continuation2) = if disc > 0.0 then continuation1(disc) else continuation2(); fun roots3(a, b, c) = let fun econt () = raise notquadratic fun ncont (x) = let fun econt () = raise imaginaryroots fun ncont (disc) = let fun con1 (disc) = [(~b + disc)/x, (~b - disc)/x] fun con2 () = [~b/x] checknumroots(disc, con1, con2) checkimaginary(b*b - 4.0*a*c, ncont, econt) checkquad(2.0*a, ncont, econt) Final version is tail recursive Continuation-Passing Form Continuation-passing form of fcn f of n values is function w/one more -- continuation f cpf (x 1,...,x n,k) = def k(f(x 1,...,x n )) So f cpf (x 1,...,x n,λy.y) = λy.y(f(x 1,...,x n )) = f(x 1,...,x n ) Use idea to derive cpf form Using Continuations Used w/multiple threads w/separate stack - Blocked thread is represented as ptr to continuation CPS transform allows to rewrite programs so no need to ever return Changing Execution Order Useful in web programming where no state.