How to force client to catch exception thrown from failed constructor

Y

yancheng.cheok

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.
 
I

Ivan Vecerina

: 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top