Topic: function try-block


Author: John Max Skaller <maxtal@suphys.physics.su.oz.au>
Date: 1995/12/11
Raw View
Avner Ben <avnerben@netvision.net.il> wrote:
>
> function-try-block:
>  try ctor-initializer-opt function-body handler-seq
>
>mean the following crazy piece of code?
>
> FileOfRecords::FileOfRecords(const char* name, int size)
>  try {
             ^ NO

>  : m_file(name), m_buffer(size)
> {
>  // do stuff
> } catch(...) {
>  // handle error
> }
        ^ NO

 .. otherwise right.

>The draft also mentions that function try blocks apply to destructor as
>well.

  Indeed: to ALL functions.

>But why call it "function try block?" can it be applied
>to plain functions as well. What is this good for?

  Well, a ctor is just a function. So why arbitrarily
restrict the syntax? It's a clean way to decouple error
handling and translation from normal processing even
in ordinary functions (IMHO).

  float f(int x)  // conversion of argument to parameter
    throw (string)
    try
      {
        // normal body....
        return i; // conversion of return value
      }
    catch(char *a)  // EH translation layer
      {
         throw string(a);
      }

I kind of like this (it was suggested by James Kanze
if I recall correctly)
--
John Max Skaller               voice: 61-2-566-2189
81 Glebe Point Rd              fax:   61-2-660-0850
GLEBE NSW 2037                 email: maxtal@suphys.physics.oz.au
AUSTRALIA                      email: skaller@maxtal.com.au



[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]






Author: Avner Ben <avnerben@netvision.net.il>
Date: 1995/12/05
Raw View
This message came out wrong in the process of transfer, so I subsmit
 it again:

The April draft mentions, in the chapter on exception handling, a
creature named __function-try-block__. The idea, if I get it right,
is to allow the constructor of an assembly class to properly handle
exceptions that might occur in the constructors of its contained
objects. Without this refinement (if the try block is inside the ctor
function block) the local handlers  will not catch it - only handlers
of the block that tried to create the object. I think this is a great
idea. Unfortuantely, the DWG refrains from bringing examples,
and I am not sure I could read the BNF precisely. Does

 function-try-block:
  try ctor-initializer-opt function-body handler-seq

mean the following crazy piece of code?

 FileOfRecords::FileOfRecords(const char* name, int size)
  try {
  : m_file(name), m_buffer(size)
 {
  // do stuff
 } catch(...) {
  // handle error
 }

The draft also mentions that function try blocks apply to destructor as
well. I can unsderstand the need for that. I also advices that the
exception handlers for function try blocks by rethrowing. This is
sensible too. But why call it "function try block?" can it be applied
to plain functions as well. What is this good for?

 Avner Ben | avnerben@netvision.net.il



---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]