How to force client to catch exception thrown from failed constructor

Discussion in 'C++' started by yancheng.cheok@gmail.com, May 15, 2006.

  1. Guest

    Hi all,

    According to "How can I handle a constructor that fails?" in
    http://www.parashift.com/c -faq-lite/exceptions.html#faq-17.2,
    whenever there is a constructor fail, we will throw exception.

    However, how can we make the interface easy to use correctly and hard
    to use incorrectly? Client may forget/ ignore from having a try...catch
    block whenever they call the constructor. Is there any way we can
    prevent this from happen?

    In Java, the language itself have the feature which force the client to
    catch the exception. How about in C++?

    Thank you.
     
    , May 15, 2006
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    : According to "How can I handle a constructor that fails?" in
    : http://www.parashift.com/c -faq-lite/exceptions.html#faq-17.2,
    : whenever there is a constructor fail, we will throw exception.
    :
    : However, how can we make the interface easy to use correctly and hard
    : to use incorrectly? Client may forget/ ignore from having a try...catch
    : block whenever they call the constructor. Is there any way we can
    : prevent this from happen?

    A well-designed C++ program has very few try...catch blocks: they
    only appear where an error will be handled by reporting it to the
    user(/external program), or where an alternative approach can be
    implemented.

    What is essential, however, is to write the code so that it is
    exception-safe: for instance, all allocated memory shall be managed
    by a container or smart-pointer, all other resources (such as
    communication ports or sockets, GUI windows, open files, etc)
    shall by managed by an owning class. This is called RAII principle,
    and is an essential coding guideline.

    Because every memory allocation, and many calls to the standard
    libraries, may throw an exception, C++ code needs to be exception-
    safe regardless of whether your constructors throw an exception
    or not.

    So the question would be: Why does a user immediately need to
    handle the exception thrown by the constructor you are talking
    about ?

    If it is a common and likely failure, remember to use exceptions
    only for "exceptional" conditions -- things that the calling code
    will legitimately expect to work (almost) every time.
    If a failure is likely, you probably want to use an alternative
    to a throwing constructor:
    - use two-step construction, with a separate e.g. "tryOpen"
    function call, which returns false in case of failure.
    - have the constructor leave the created instance in a
    resource-less or incompletely constructed state that the
    user can check for. (an exception will then only be
    thrown if/when an operation is attempted using that instance).


    : In Java, the language itself have the feature which force the client to
    : catch the exception. How about in C++?

    C++ has throw-specifications just like Java. But their consistency
    is not strictly enforced by the compiler. In practice, most experts
    agree that only an empty throw-specification is useful in C++ (to
    indicate that a function will never throw).
    [ Java's model has it's problems too, but I won't get into this... ]


    Regards,
    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
     
    Ivan Vecerina, May 15, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Selen
    Replies:
    0
    Views:
    2,722
    Selen
    May 28, 2004
  2. Fao, Sean
    Replies:
    5
    Views:
    424
    Fao, Sean
    Jul 6, 2003
  3. drop
    Replies:
    0
    Views:
    415
  4. Philipp
    Replies:
    14
    Views:
    1,238
    Daniel Pitts
    Apr 26, 2008
  5. Marteno Rodia

    catch doesn't catch a thrown exception

    Marteno Rodia, Aug 3, 2009, in forum: Java
    Replies:
    5
    Views:
    627
    Daniel Pitts
    Aug 5, 2009
Loading...

Share This Page