Re: exiting a constructor early

From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 03/12/04


Date: Sat, 13 Mar 2004 00:19:20 +1100


"Francis Glassborow" <francis@robinton.demon.co.uk> wrote in message
news:1aK0yW0mDbUAFwND@robinton.demon.co.uk...
| In message <c2sacu$21pvvl$1@ID-110726.news.uni-berlin.de>, "Chris ( Val
| )" <chrisval@bigpond.com.au> writes
| >| Function try blocks actually add very little and are rarely if ever
| >| needed in well designed code.
| >
| >Maybe - but, if dealing with exceptions in an constructor,
| >then the function try block is the prefered construct to use :-).
|
| I think not.

Why not ?

Where is a better place to catch exceptions thrown by any
resource allocation in an initialiser list ?

| Once you get into a catch part of a function try block you
| are outside the function and the exception will propagate.

Yes, but the function try block mechanism allows you to 're-throw'
the exception object, and to 're-throw' a different object if you
wish, allowing you to legally break an exception specification.

| If you want to handle an exception in the body of a ctor, do so
| IN THE BODY.

With the function try block syntax though, the 'try' scope *is*
the body of the constructor - the handler is for handling the
exception object - the constructor body need not do it.

| If you do not want to then let it propagate. If you find yourself
| having to do some external handling via a function try block, look
| at your design and ask yourself why that is the case. I think the
| usual answer will be that the class has been poorly designed.

I'm not sure what you're talking about. AFAIU, the function try block
construct is an intimate part of the constructor itself when used, and
it does offer advantages as I've noted.

Cheers.
Chris Val



Relevant Pages

  • Re: is such exception handling approach good?
    ... There is nothing wrong with throwing from constructor. ... It may be a good design, it may not be a good design from user's point ... resource API to free-up the resource on exception. ... change something - that is not initialization. ...
    (microsoft.public.vc.language)
  • Re: is such exception handling approach good?
    ... It may be a good design, it may not be a good design from user's point ... doesn't get the connection created automatically on object creation. ... instance you use a class somebody else wrote which throws an exception ... constructor code would be shared... ...
    (microsoft.public.vc.language)
  • RE: inheritance trouble - how to detect run-time or design-time in code
    ... The base class will throw a exception its ... VS.NET's design view again, yes? ... However, as for the Constructor, we're unable to use the above means to ... Move the code into page's OnInit method and use the ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: exception handling .. more
    ... | is exception safe. ... have caught the exception during it's initialisation phase, ... becomes an extension of the constructor ... try block mechanism allows you to 're-throw' the exception object, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: is such exception handling approach good?
    ... There is nothing wrong with throwing from constructor. ... an oracle database in the ctor then what should you do with the object ... It may be a good design, it may not be a good design from user's point ... resource API to free-up the resource on exception. ...
    (microsoft.public.vc.language)