Re: Console application termination following exception thrown in constructor (VS 2005)
- From: "Paul" <vhr@xxxxxxxxxxxxxxxxx>
- Date: Wed, 17 Jan 2007 11:58:35 -0000
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.
.
- Follow-Ups:
- Re: Console application termination following exception thrown in constructor (VS 2005)
- From: Charles Wang[MSFT]
- Re: Console application termination following exception thrown in constructor (VS 2005)
- References:
- Console application termination following exception thrown in constructor (VS 2005)
- From: Paul
- RE: Console application termination following exception thrown in constructor (VS 2005)
- From: Charles Wang[MSFT]
- Re: Console application termination following exception thrown in constructor (VS 2005)
- From: Paul
- Re: Console application termination following exception thrown in constructor (VS 2005)
- From: Charles Wang[MSFT]
- Console application termination following exception thrown in constructor (VS 2005)
- Prev by Date: Re: Edit binary DLL to change time zone rules
- Next by Date: Re: Must compile all with _SECURE_SCL setting?
- Previous by thread: Re: Console application termination following exception thrown in constructor (VS 2005)
- Next by thread: Re: Console application termination following exception thrown in constructor (VS 2005)
- Index(es):
Relevant Pages
|