Re: Console application termination following exception thrown in constructor (VS 2005)



Could you please let me know why you use "try... catch" like this? It is
not a common usage.
I think that such usage may be dangerous, since if external "try...catch"
block statement is not added, the application may crash. Does it have a
special usage in your situation?

I'd say the usage is sufficiently common. Constructors may throw exceptions
and Stroustrup gives examples of such practice (see E.3.5.1 Using init()
Functions, p. 950 and, say, D.2.1 Named Locales, p. 875: "If the string
argument doesn't refer to a defined locale, the constructor throws the
runtime_error exception..."; both references are from "The C++ Programming
Language").

Regardless of the above, in my case the third-party library class whose
constructor requires an argument to be passed in may throw an exception and
personally I am not aware of any method to initialise such a data member
other than outside the main constructor body:

MyClass::MyClass(const ThirdPartyConstructorArgument& arg)
: third_party_library_class_data_member(arg) {}

To catch the exception in this case, the construct I used is required (see
Stroustrup, p. 373).

An alternative would be to put the MyClass construction in a try-block:

try {
MyClass c;
}
catch (...) {
std::cerr << "Exception goes here.\n";
}

but to catch the specific exception that the third-party class may throw I
would need the type of that exception or, in other words, an extra header
(extra #include) in the translation unit where MyClass is created and used
which in this case, to my mind, would serve no purpose.

Also, I would like to know if the dialogue error box persists now at your
side. I can run your code normally under my machine.

No, it no longer appears, so the code is now all right.


.



Relevant Pages

  • Re: DbC & Exceptions & Style
    ... exception. ... Different ways the constructor can fail ... to avoid writing code that converts GUI data into objects, ... All domain objects must now provide a default constructor. ...
    (comp.object)
  • Re: Controlled types and exception safety
    ... >> propagate an exception. ... >> For an Adjust invoked as part of an assignment operation, ... But a user-defined constructor is ... a user-defined constructor has just turned on the ...
    (comp.lang.ada)
  • 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: Question concerning object-oriented programming
    ... constructor the inspectof the state fails to verify key ... Better yet the constructor could throw an exception. ... opposed to stateless) object can know if it is in an invalid state. ...
    (comp.programming)
  • Re: Why does std::exception constructor specifies it can throw any ?
    ... > specifies that the constructors could throw any exception becuase they ... The constructor, copy constructor, operator=, ... DerivedException() throw ... contained objects don't throw. ...
    (comp.lang.cpp)