How to force client to catch exception thrown from failed constructor
According to "How can I handle a constructor that fails?" in
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++?
Re: How to force client to catch exception thrown from failed constructor
<email@example.com> wrote in message
: According to "How can I handle a constructor that fails?" in
: 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
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
So the question would be: Why does a user immediately need to
handle the exception thrown by the constructor you are talking
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... ]
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
|All times are GMT. The time now is 11:10 AM.|
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.