home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16507 < prev    next >
Encoding:
Text File  |  1992-11-18  |  2.6 KB  |  64 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!elroy.jpl.nasa.gov!ames!haven.umd.edu!darwin.sura.net!jvnc.net!yale.edu!ira.uka.de!slsvaat!josef!kanze
  3. From: kanze@us-es.sel.de (James Kanze)
  4. Subject: Re: Two things: typeof() and exceptions
  5. In-Reply-To: mccauleyba@vax1.bham.ac.uk's message of Mon, 16 Nov 1992 20:20:36 GMT
  6. Message-ID: <KANZE.92Nov18154311@slsvdnt.us-es.sel.de>
  7. Sender: news@us-es.sel.de
  8. Organization: SEL
  9. References: <MCGRANT.92Nov15134127@rascals.stanford.edu>
  10.     <1992Nov16.202036.1@vax1.bham.ac.uk>
  11. Date: 18 Nov 92 15:43:11
  12. Lines: 50
  13.  
  14. In article <1992Nov16.202036.1@vax1.bham.ac.uk>, Brian McCauley
  15. writes:
  16.  
  17. |> > Secondly, I was reading the reference manual for Dylan (Apple's new OO
  18. |> > language) and I noticed something that was pretty hot in the exception-
  19. |> > handling domain. Basically, in any given procedure you can define a set
  20. |> > of instructions that are guaranteed to be executed even if the rest of
  21. |> > the procedure is terminated early due to an exception. Currently in C++,
  22. |> > automatic variables are destroyed as the stack is unwound. But, with 
  23. |> > a set of 'unwind' statements in a function, I could destroy all of the
  24. |> > variables I allocated on the heap as well.
  25.  
  26. |> Yes? In C++ this is written thus:
  27.  
  28. |> void foo() {
  29. |>   try { /* body of function */ }
  30. |>   catch (...) { /* unwinding code */ throw; }
  31. |> }
  32.  
  33. |> I'm sure the answer to your question can't be that simple so perhaps
  34. |> you'd better explain again.
  35.  
  36. I don't know what Dylan does, but Modula-3 has a finally clause in
  37. addition to a catch for a try block.
  38.  
  39. The difference is: a catch clause is only executed if an exception is
  40. thrown.  If it is used for ressource clean-up, then the clean up code
  41. must be duplicated outside of the catch clause.  A finally clause is
  42. executed whether or not an exception is thrown, so it is sufficient to
  43. put any clean-up code there.  (It also rethrows any exception
  44. automatically, so the "throw" isn't necessary either.)
  45.  
  46. Would there be any support for adding a finally clause to C++.  It's
  47. not likely to break any existing code, since it is a pure extension
  48. (to exceptions, no less; how many people are actually using exceptions
  49. at this time, as opposed to the number who wish they could?).  Except
  50. for the new keyword, I don't see anything that it could break (just
  51. offhand, I haven't done any real analysis).
  52.  
  53. I know that the same effect can be obtained by declaring anything
  54. requiring clean-up as a class, and doing the clean-up in the
  55. destructor, but I'm not sure that this is always the most appropriate
  56. idiom.
  57. --
  58. James Kanze            GABI Software, Sarl.
  59. email: kanze@us-es.sel.de    8 rue du Faisan
  60.                 67000 Strasbourg
  61.                 France
  62.  
  63.  
  64.