home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / arch / 10542 < prev    next >
Encoding:
Internet Message Format  |  1992-11-08  |  3.0 KB

  1. Xref: sparky comp.arch:10542 comp.lang.misc:3548
  2. Newsgroups: comp.arch,comp.lang.misc
  3. Path: sparky!uunet!elroy.jpl.nasa.gov!decwrl!pa.dec.com!rdg.dec.com!jch
  4. From: jch@rdg.dec.com (John Haxby)
  5. Subject: Re: CLU break and continue (Was: Re: A challenge to the anti-goto)
  6. Message-ID: <1992Nov9.114323.22688@rdg.dec.com>
  7. Sender: news@rdg.dec.com (Mr News)
  8. Organization: Digital Equipment Corporation
  9. References: <1cvoctINNmhs@agate.berkeley.edu> <PSM.92Nov3165530@soma.sics.se> <1992Nov3.165005.18037@midway.uchicago.edu> <184123@pyramid.pyramid.com> <721054332.26680@minster.york.ac.uk>
  10. Date: Mon, 9 Nov 1992 11:43:23 GMT
  11. Lines: 58
  12.  
  13. In article <721054332.26680@minster.york.ac.uk>, mjl-b@minster.york.ac.uk writes:
  14. |> >You're probably thinking of the `exit'/`except' mechanism, which allows
  15. |> >you to escape from multiple loops (or from within a `proc', with the
  16. |> >`signal' statement) by raising a condition which transfers control to
  17. |> >the nearest enclosing handler for that condition.
  18. |> 
  19. |> This sounds a lot like the Ada exception mechanism. Briefly, when an
  20. |> exception is raised, the run-time starts looking for a handler for that
  21. |> particular exception. If there isn't one for that exception in the current
  22. |> block, the call stack is wound back until one is found, or the top-level
  23. |> subprogram is reached (when the program stops).
  24.  
  25. It's not surprising that the Ada exception mechanism is similar to CLU's
  26. since CLU is one of Ada's ancestors.
  27.  
  28. CLU has two exception mechanisms for slightly different purposes.  One
  29. mechanism is for returning an exception from a procedure call: a procedure
  30. can signal an exception to be caught by its caller (the exception is not propagated
  31. up the procedure call stack) or by a generic handler for unhandled exceptions.  The
  32. other mechanism is a local form that allows a block (eg in a loop or if-statement) to
  33. exit to an enclosing block.  Apart from the local/procedure-call difference the two
  34. mechanisms are identical, although `signal' tends to be expensive and `exit' is simply
  35. an unconditional branch.  For example:
  36.  
  37.     ...
  38.         x := sum (n)
  39.             except when negative: x := 0 end
  40.     ....
  41.  
  42.     sumOfInts = proc (n: int) signals (negative)
  43.         if n < 0 then
  44.             signal negative
  45.             end
  46.         sum: int := 0
  47.         while true do
  48.             sum := sum + n
  49.             n := n - 1
  50.             if n <= 0 then
  51.                 exit done end
  52.             end except when done: return (sum) end
  53.         end sumOfInts
  54.  
  55. The example is a little contrived I'm afraid.  Both sorts of exception can be
  56. parameterized.  For example, procedures that map on to system calls often signal
  57. not_possible(string) -- the string is the string representation of the error code
  58. from the routine when it fails.
  59.  
  60. Liskov and Guttag's "Abstraction and Specification in Program Design",
  61. already cited, is much better at explaining this than I am on a Monday
  62. morning.
  63.  
  64. -- 
  65. John Haxby, Definitively Wrong.
  66. Digital                <jch@rdg.dec.com>
  67. Reading, England        <...!uknet!wessex!jch>
  68.  
  69. ----------------------------------------------------------------
  70. The opinions expressed herein are my own, not my employers.
  71.