Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to force client to catch exception thrown from failed constructor

Reply
Thread Tools

How to force client to catch exception thrown from failed constructor

 
 
yancheng.cheok@gmail.com
Guest
Posts: n/a
 
      05-15-2006
Hi all,

According to "How can I handle a constructor that fails?" in
http://www.parashift.com/c++-faq-lit....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.

 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      05-15-2006
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
: According to "How can I handle a constructor that fails?" in
: http://www.parashift.com/c++-faq-lit....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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
catch doesn't catch a thrown exception Marteno Rodia Java 5 08-05-2009 03:30 AM
Nike air force one, air force 1, air force one low cut, air force one abdul_razak@indiatimes.com Digital Photography 2 12-31-2008 04:29 PM
Exception of type 'System.Web.HttpUnhandledException' wasthrown.Exception has been thrown by the target of an invocation.System.WebSystem.Exception jobs ASP .Net 1 11-16-2007 05:57 PM
Proper Way to catch exception thrown by new keyword Fao, Sean C++ 5 07-06-2003 05:21 PM



Advertisments