CS317 Concepts of Programming Languages

28
slide 1 Dr. Mohammad El-Ramly Fall 2010 Set 8.1 – Control Flow and Exceptions I Slides by John Mitchell and Vitaly Shmatikov Cairo University Faculty of Computers and Information CS317 Concepts of Programming Languages Reading: “Concepts in Programming Languages”, Chapter 8.1 – 8.2

description

Cairo University Faculty of Computers and Information. CS317 Concepts of Programming Languages. Dr. Mohammad El-Ramly Fall 2010 Set 8.1 – Control Flow and Exceptions I Slides by John Mitchell and Vitaly Shmatikov. - PowerPoint PPT Presentation

Transcript of CS317 Concepts of Programming Languages

Page 1: CS317  Concepts of Programming Languages

slide 1

Dr. Mohammad El-RamlyFall 2010

Set 8.1 – Control Flow and Exceptions ISlides by John Mitchell and Vitaly Shmatikov

Cairo University Faculty of Computers and Information

CS317 Concepts of Programming Languages

Reading: “Concepts in Programming Languages”, Chapter 8.1 – 8.2

Page 2: CS317  Concepts of Programming Languages

slide 2

Quote of the Day

“The primary duty of an exception handler is to get the error out of the lap of the programmer and into the surprised face of the user.” - Verity Stob

Page 3: CS317  Concepts of Programming Languages

Topics Structured Programming

• Go to considered harmful Exceptions

• “structured” jumps that may return a value• dynamic scoping of exception handler

Continuations• Function representing the rest of the program• Generalized form of tail recursion

Discussion of functional programming• Relevant to Haskell, section 4.4 of text

Page 4: CS317  Concepts of Programming Languages

8.1 Fortran Control Structure10 IF (X .GT. 0.000001) GO TO 2011 X = -X IF (X .LT. 0.000001) GO TO 5020 IF (X*Y .LT. 0.00001) GO TO 30 X = X-Y-Y30 X = X+Y ...50 CONTINUE X = A Y = B-A GO TO 11 …

•Similar structure may occur in assembly code

Page 5: CS317  Concepts of Programming Languages

Historical Debate Dijkstra, Go To Statement Considered Harmful

• Letter to Editor, C ACM, March 1968• Link on CS242 web site

Knuth, Structured Prog. with go to Statements• You can use goto, but do so in structured way …

Continued discussion• Welch, “GOTO (Considered Harmful)n, n is Odd”

General questions• Do syntactic rules force good programming style?• Can they help?

Page 6: CS317  Concepts of Programming Languages

Advance in Computer Science Standard constructs that structure jumps

if … then … else … endwhile … do … endfor … { … }case …

Modern style• Group code in logical blocks • Avoid explicit jumps except for function return• Cannot jump into middle of block or function

body

Page 7: CS317  Concepts of Programming Languages

slide 7

8.2.1 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 (exception catching)

• Statement or expression to raise or throw exception

Often used for unusual or exceptional condition, but not necessarily

Page 8: CS317  Concepts of Programming Languages

slide 8

Why Exceptions? Exceptions became a widely accepted

language construct for a number of reasons.• Many languages do not otherwise have a clean

construct for aborting (jumping out) a function call.

• Exceptions also allow passing data as part of the jump.

• Exceptions provide a way for determining to where the jump goes.

Often used for unusual or exceptional condition, but not necessarily

Page 9: CS317  Concepts of Programming Languages

slide 9

ML Exampleexception 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

Page 10: CS317  Concepts of Programming Languages

slide 10

C++ ExampleMatrix invert(Matrix m) {

if … throw Determinant;…

};

try { … invert(myMatrix); …}catch (Determinant) { …

// recover from error}

Page 11: CS317  Concepts of Programming Languages

slide 11

C++ vs ML Exceptions C++ exceptions

• Can throw any type, but C++ father recommends:• 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, 3rd ed.

ML exceptions• Exceptions are a different kind of entity than types• Declare exceptions before useSimilar, but ML requires while C++ only recommends

Page 12: CS317  Concepts of Programming Languages

slide 12

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 insteadGeneral form allows multiple patterns

Page 13: CS317  Concepts of Programming Languages

slide 13

8.2.2 ML Exceptions exception Ovflw; exception Signal of int;

raise Ovflw; raise Signal (x + 4);

Page 14: CS317  Concepts of Programming Languages

Which handler is used?

Note:• First call handles exception one way• Second call handles exception in a different way

• exception Ovflw;• fun reciprocal(x) = • if x<min then raise Ovflw else 1/x;• (reciprocal(x) handle Ovflw=>0) / (reciprocal(y) handle

Ovflw=>1);•try •try•catch •catch

•throw

Page 15: CS317  Concepts of Programming Languages

A handler with pattern matching exception Signal of int; fun f(x) = if x = 0 then raise Signal (0)

else if x = 1 then raise Signal (1)else if x = 10 then raise Signal (x-8)else (x – 2) mod 4;

f(10) handle Signal (0) => 0| Signal (1) => 1| Signal (x) => x + 8;

Page 16: CS317  Concepts of Programming Languages

8.2.3 C++ Exception try { // code that may throw an exception throw “Generating char * exception”}

catch (char * message) { // process the exception }

C++ uses types to differentiate between different types of exceptions.

Page 17: CS317  Concepts of Programming Languages

C++ Exception throw “Hello World” // throws a char * throw 18 // throws int throw new String (“Hello”);

catch (char * message) {// process the char * exception

} catch (void *whatever) {// process all other pointer exceptions

}

Page 18: CS317  Concepts of Programming Languages

slide 18

C++ vs ML Exceptions C++ exceptions

• Can throw any type• No need to declare exceptions beforehand• Uses type matching to determine the right handler• Not garbage collected. Some objects may become

inaccessible. ML exceptions

• Exceptions are a different kind of entity than types• Declare exceptions before use• Uses pattern matching to determine the right

handler • Inaccessible objects are deallocated.

Page 19: CS317  Concepts of Programming Languages

8.2.4 Exception for Error Condition

- datatype 'a tree = Leaf of 'a | Node of ('a tree)*('a tree);

- exception No_Subtree;- fun lsub (Leaf x) = raise No_Subtree | lsub (Node (x,y)) = x;> val lsub = fn : ‘a tree -> ‘a tree

• This function raises an exception when there is no reasonable value to return

• We’ll look at typing later.

Page 20: CS317  Concepts of Programming Languages

slide 20

Exceptions for Efficiency Function to multiply values of tree leaves

fun prod(Leaf x) = x| prod(Node(x,y)) = prod(x) * prod(y);

Optimize using exception fun prod(tree) = let exception Zero fun p(Leaf x) = if x=0 then (raise Zero) else x | p(Node (x,y)) = p(x) * p(y) in p(tree) handle Zero=>0 end;

Page 21: CS317  Concepts of Programming Languages

slide 21

Dynamic Scoping of Handlersexception 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

Page 22: CS317  Concepts of Programming Languages

slide 22

Scope of Exception Handlers

exception X;(let fun f(y) = raise X and g(h) = h(1) handle X => 2in g(f) handle X => 4end) handle X => 6;

scope

handler

Which handler is used?

Page 23: CS317  Concepts of Programming Languages

slide 23

exception X;fun f(y) = raise Xfun g(h) = h(1) handle X => 2g(f) handle X => 4

Dynamic Scope of Handlers (1)

formal hhandler X 2

access link

formal y 1access link

g(f)

f(1)

fun f access link

access link fun g

Dynamic scope:find first X handler, going up the dynamic call chain leading to “raise X”

handler X 4access link

Page 24: CS317  Concepts of Programming Languages

slide 24

Dynamic Scope of Handlers (2)exception X;(let fun f(y) = raise X and g(h) = h(1) handle X =>

2in g(f) handle X => 4end) handle X => 6;

handler X 6

formal hhandler X 2

access link

formal y 1access link

g(f)

f(1)

fun f access link

access link fun g

Dynamic scope: find first X handler, going up the dynamic call chain leading to “raise X”

handler X 4access link

Page 25: CS317  Concepts of Programming Languages

slide 25

Scoping: Exceptions vs. Variablesexception X;(let fun f(y) = raise X and g(h) = h(1) handle X => 2in g(f) handle X => 4end) 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);

Page 26: CS317  Concepts of Programming Languages

slide 26

Static Scope of Declarationsval 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 x 6

formal hval x 2

access link

formal y 1access link

g(f)

f(1)

fun f access link

access link fun g

Static scope: find first x, following access links from the reference to X

val x 4access link

Page 27: CS317  Concepts of Programming Languages

slide 27

8.2.5 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 + (e1 handle X => e2) Type of e1,e2 must be int

(why?)

Page 28: CS317  Concepts of Programming Languages

slide 28

Exceptions and Resource Allocation

exception X;(let val x = ref [1,2,3] in let val y = ref [4,5,6] in … raise X endend); 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