1 / 28

CS317 Concepts of Programming Languages

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.

cora
Download Presentation

CS317 Concepts of Programming Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 Reading: “Concepts in Programming Languages”, Chapter 8.1 – 8.2

  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

  3. 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

  4. 8.1 Fortran Control Structure 10 IF (X .GT. 0.000001) GO TO 20 11 X = -X IF (X .LT. 0.000001) GO TO 50 20 IF (X*Y .LT. 0.00001) GO TO 30 X = X-Y-Y 30 X = X+Y ... 50 CONTINUE X = A Y = B-A GO TO 11 … • Similar structure may occur in assembly code

  5. 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?

  6. Advance in Computer Science • Standard constructs that structure jumps if … then … else … end while … do … end for … { … } case … • Modern style • Group code in logical blocks • Avoid explicit jumps except for function return • Cannot jump into middle of block or function body

  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

  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

  9. 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

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

  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 use Similar, but ML requires while C++ only recommends

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

  13. 8.2.2 ML Exceptions exception Ovflw; exception Signal of int; raise Ovflw; raise Signal (x + 4);

  14. Which handler is used? • exception Ovflw; • fun reciprocal(x) = • if x<min then raise Ovflw else 1/x; • (reciprocal(x) handle Ovflw=>0) / (reciprocal(y) handle Ovflw=>1); • Note: • First call handles exception one way • Second call handles exception in a different way • throw • try • catch • try • catch

  15. 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;

  16. 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.

  17. 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 }

  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.

  19. 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; > vallsub = fn : ‘a tree -> ‘a tree • This function raises an exception when there is no reasonable value to return • We’ll look at typing later.

  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;

  21. 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

  22. 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?

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

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

  25. 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);

  26. 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); access link access link val x val x access link formal h fun f access link access link fun g val x formal y 1 4 2 6 Static scope: find first x, following access links from the reference to X g(f) f(1)

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

  28. 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 Exceptions and Resource Allocation General problem, no obvious solution

More Related