Topic: try block as main block of a function
Author: Mike Schilling <mike.schilling@ebay.sun.com>
Date: Mon, 6 Aug 2001 19:52:51 GMT Raw View
Anthony Williams wrote:
> For functions [a try-block as the body of a function]
> saves a level of indentation and a pair of braces, but
> otherwise is identical to a function body containing nothing but a try
> block. The only reason I can see for not recommending it is portability
> (e.g.Daniel Garc=EDa's original problem wrt MSVC).
It's also a rare construct, confusing to anyone who hasn't seen it
before, and will cause problems using editors which know that a function
begins with a brace in column 1, as well as other code analysis tools.=20
IMHO, all good reasons not to recommend it.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Mon, 30 Jul 2001 16:06:18 GMT Raw View
In article <9jtbcc$104r4$1@sky.inp.nsk.su>, Andy Salnikov
<A.A.Salnikov@inp.nsk.su> writes
>"Francis Glassborow" <francis.glassborow@ntlworld.com> wrote in message
>news:OOB2fjAifdY7Ewle@ntlworld.com...
>> In article <9jr8bd$19vqd$1@ID-49767.news.dfncis.de>, Anthony Williams
>> <anthwil@nortelnetworks.com> writes
>> >Why would you not advocate using function try blocks?
>>
>> Because many will forget that they automatically rethrow if you do not
>> do so. I hate default behaviour as it often hides errors.
>>
> Is my reading of 15.3/16 incorrect? It seems to imply that what you are
>writing about is applicable to constructors and destructors only, but normal
>functions should simply return and not rethrow.
That is my point, they do not. If you wrap a function in a try block and
an exception is thrown the function does not return after processing an
exception but rethrows.
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Mon, 30 Jul 2001 20:44:26 GMT Raw View
In article <9k34ms$2akk5$1@ID-49767.news.dfncis.de>, Anthony Williams
<anthwil@nortelnetworks.com> writes
>8.4p16 says:
>"The exception being handled is rethrown if control reaches the end of a
>handler of the function-try-block of a constructor or destructor. Otherwise,
>a function returns when control reaches the end of a handler for the
>function-try-block (6.6.3). Flowing off the end of a function-try-block is
>equivalent to a return with no value; this results in undefined behavior in
>a value-returning function (6.6.3)."
You are right. But that seems adequate for not using them except for
ctors (only fools let exception escape from dtors :)
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Anthony Williams" <anthwil@nortelnetworks.com>
Date: Tue, 31 Jul 2001 17:05:50 GMT Raw View
"Francis Glassborow" <francis.glassborow@ntlworld.com> wrote in message
news:+5FKSWA1iaZ7Ew4q@ntlworld.com...
> In article <9k34ms$2akk5$1@ID-49767.news.dfncis.de>, Anthony Williams
> <anthwil@nortelnetworks.com> writes
> >8.4p16 says:
> >"The exception being handled is rethrown if control reaches the end of a
> >handler of the function-try-block of a constructor or destructor.
Otherwise,
> >a function returns when control reaches the end of a handler for the
> >function-try-block (6.6.3). Flowing off the end of a function-try-block
is
> >equivalent to a return with no value; this results in undefined behavior
in
> >a value-returning function (6.6.3)."
>
> You are right. But that seems adequate for not using them except for
> ctors (only fools let exception escape from dtors :)
You said (in an earlier message)
>Because many will forget that they automatically rethrow if you do not
>do so. I hate default behaviour as it often hides errors.
It seems to me that the only situation in which you would recommend using
function-try-blocks is the one case in which the default behaviour that you
"hate" happens, so I don't understand your position.
The only case in which rethrowing occurs is for constructors/destructors. I
agree that permitting exceptions to escape destructors is foolish. You agree
that using function try blocks with constructors is permissible, even though
this is therefore the only place where the "rethrow" default behaviour will
show up.
For normal functions the default behaviour is what you would naively expect,
exceptions are not rethrown - you just have to ensure you code a return
statement (for a value-returning-function) in the catch handler, which you
would have to do anyway even if the catch was within the block
int func()
try
{
// do something
}
catch(...)
{
// do some error handling
}
is _identical_ in behaviour to
int func()
{
try
{
// do something
}
catch(...)
{
// do some error handling
}
}
Anthony
--
Anthony Williams
Software Engineer, Nortel Networks Optoelectronics
The opinions expressed in this message are not necessarily those of my
employer
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Andy Salnikov" <A.A.Salnikov@inp.nsk.su>
Date: Tue, 31 Jul 2001 17:05:44 GMT Raw View
"Francis Glassborow" <francis.glassborow@ntlworld.com> wrote in message
news:hSy9obBruRZ7EwIG@ntlworld.com...
> That is my point, they do not. If you wrap a function in a try block and
> an exception is thrown the function does not return after processing an
> exception but rethrows.
>
This sounds strange and almost unreasonable. Where is the place in
standard which says so?
The following simple program does what I would expect, i.e. prints both
lines to cout:
#include <iostream>
#include <stdexcept>
void f()
try {
throw std::logic_error("no logic here") ;
} catch ( const std::exception& e ) {
std::cout << "exception caught: " << e.what() << std::endl ;
}
int main(int,char**)
{
f() ;
std::cout << "returned from f()" << std::endl ;
}
Cheers,
Andy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Wed, 1 Aug 2001 11:02:21 GMT Raw View
In article <9k5f5h$16cuv$1@sky.inp.nsk.su>, Andy Salnikov
<A.A.Salnikov@inp.nsk.su> writes
>"Francis Glassborow" <francis.glassborow@ntlworld.com> wrote in message
>news:hSy9obBruRZ7EwIG@ntlworld.com...
>> That is my point, they do not. If you wrap a function in a try block and
>> an exception is thrown the function does not return after processing an
>> exception but rethrows.
>>
> This sounds strange and almost unreasonable. Where is the place in
>standard which says so?
There isn't (it only applies to ctors and dtors) However you do need to
include a return statement in every catch clause if the function returns
anything but void. Many frown on multiple return statements in a
function.
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Serge Smeesters" <sergesmeesters@netcourrier.com>
Date: Fri, 27 Jul 2001 17:03:04 GMT Raw View
> Could anybody tell me if this is legal ?
>
> void f()
> try {
> do_something();
> }
> catch (...) {
> handle_my_errors();
> }
>
> Or should I write?
>
> void f()
> {
> try {
> do_something();
> }
> catch (...) {
> handle_my_errors();
> }
> }
>
> I've seen the former in a book, but my compiler (MSVC++ 6.0) only
> accepts the latter.
Maybe your are confusing with function try blocks useful for some constructor like :
class X {};
C::C(const std::string & a_name ) throw ( X ) // allowed to throw X only
try
: m_name( a_name ) // m_name's constructor might throw a bad_alloc exception,
// might violate C's exception specification
{
// constructor function body
}
catch( ... ) // handle any exception throw from ctor initialize or ctor body
{
// ...
throw X(); // replace bad_alloc exception with an exception of type X
}
--
Serge Smeesters
http://www.sesa.ucl.ac.be/serge/
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Anthony Williams" <anthwil@nortelnetworks.com>
Date: Fri, 27 Jul 2001 17:39:43 GMT Raw View
"Francis Glassborow" <francis.glassborow@ntlworld.com> wrote in message
news:cKmyuWA$AJY7EwG1@ntlworld.com...
> In article <3b6061b5.1188820@news.terra.es>, J. Daniel Garcia Sanchez
> <jdaniel.garcia@terra.es> writes
> >Could anybody tell me if this is legal ?
> >
> >void f()
> >try {
> > do_something();
> >}
> >catch (...) {
> > handle_my_errors();
> >}
> >
> >Or should I write?
> >
> >void f()
> >{
> > try {
> > do_something();
> > }
> > catch (...) {
> > handle_my_errors();
> > }
> >}
> >
> >I've seen the former in a book, but my compiler (MSVC++ 6.0) only
> >accepts the latter.
>
> Well they are actually different things, and I would not advocate the
> version that wraps an entire function in a try block (it fell out of the
> grammar change to allow wrapping a ctor-init list in a try block, which
> is sometimes necessary. I am not surprised that an old compiler like
> VC++6 doesn't understand it (and I would not blame MS for that).
Why would you not advocate using function try blocks?
Anthony
--
Anthony Williams
Software Engineer, Nortel Networks Optoelectronics
The opinions expressed in this message are not necessarily those of my
employer
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Fri, 27 Jul 2001 21:11:16 GMT Raw View
In article <9jr8bd$19vqd$1@ID-49767.news.dfncis.de>, Anthony Williams
<anthwil@nortelnetworks.com> writes
>Why would you not advocate using function try blocks?
Because many will forget that they automatically rethrow if you do not
do so. I hate default behaviour as it often hides errors.
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Andy Salnikov" <A.A.Salnikov@inp.nsk.su>
Date: Mon, 30 Jul 2001 03:16:57 GMT Raw View
"Francis Glassborow" <francis.glassborow@ntlworld.com> wrote in message
news:OOB2fjAifdY7Ewle@ntlworld.com...
> In article <9jr8bd$19vqd$1@ID-49767.news.dfncis.de>, Anthony Williams
> <anthwil@nortelnetworks.com> writes
> >Why would you not advocate using function try blocks?
>
> Because many will forget that they automatically rethrow if you do not
> do so. I hate default behaviour as it often hides errors.
>
Is my reading of 15.3/16 incorrect? It seems to imply that what you are
writing about is applicable to constructors and destructors only, but normal
functions should simply return and not rethrow.
>
> Francis Glassborow ACCU
> 64 Southfield Rd
> Oxford OX4 1PA +44(0)1865 246490
> All opinions are mine and do not represent those of any organisation
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Anthony Williams" <anthwil@nortelnetworks.com>
Date: Mon, 30 Jul 2001 10:32:35 GMT Raw View
"Francis Glassborow" <francis.glassborow@ntlworld.com> wrote in message
news:OOB2fjAifdY7Ewle@ntlworld.com...
> In article <9jr8bd$19vqd$1@ID-49767.news.dfncis.de>, Anthony Williams
> <anthwil@nortelnetworks.com> writes
> >Why would you not advocate using function try blocks?
>
> Because many will forget that they automatically rethrow if you do not
> do so. I hate default behaviour as it often hides errors.
8.4p16 says:
"The exception being handled is rethrown if control reaches the end of a
handler of the function-try-block of a constructor or destructor. Otherwise,
a function returns when control reaches the end of a handler for the
function-try-block (6.6.3). Flowing off the end of a function-try-block is
equivalent to a return with no value; this results in undefined behavior in
a value-returning function (6.6.3)."
Thus it seems that function try blocks only have this behaviour in
constructors and destructors. I'll go with not recommending them for
destructors, but their use with constructors is the reason they were
introduced in the first place (as you said in your original reply). For
functions it saves a level of indentation and a pair of braces, but
otherwise is identical to a function body containing nothing but a try
block. The only reason I can see for not recommending it is portability
(e.g.Daniel Garc a's original problem wrt MSVC).
Anthony
--
Anthony Williams
Software Engineer, Nortel Networks Optoelectronics
The opinions expressed in this message are not necessarily those of my
employer
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: jdaniel.garcia@terra.es (J. Daniel Garcia Sanchez)
Date: Thu, 26 Jul 2001 21:12:40 GMT Raw View
Could anybody tell me if this is legal ?
void f()
try {
do_something();
}
catch (...) {
handle_my_errors();
}
Or should I write?
void f()
{
try {
do_something();
}
catch (...) {
handle_my_errors();
}
}
I've seen the former in a book, but my compiler (MSVC++ 6.0) only
accepts the latter.
Regards
--
Daniel Garc a
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Thu, 26 Jul 2001 21:48:04 GMT Raw View
In article <3b6061b5.1188820@news.terra.es>, J. Daniel Garcia Sanchez
<jdaniel.garcia@terra.es> writes
>Could anybody tell me if this is legal ?
>
>void f()
>try {
> do_something();
>}
>catch (...) {
> handle_my_errors();
>}
>
>Or should I write?
>
>void f()
>{
> try {
> do_something();
> }
> catch (...) {
> handle_my_errors();
> }
>}
>
>I've seen the former in a book, but my compiler (MSVC++ 6.0) only
>accepts the latter.
Well they are actually different things, and I would not advocate the
version that wraps an entire function in a try block (it fell out of the
grammar change to allow wrapping a ctor-init list in a try block, which
is sometimes necessary. I am not surprised that an old compiler like
VC++6 doesn't understand it (and I would not blame MS for that).
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]