Re: Console application termination following exception thrown in constructor (VS 2005)
- From: "Tom Widmer [VC++ MVP]" <tom_usenet@xxxxxxxxxxx>
- Date: Mon, 15 Jan 2007 12:42:26 +0000
Paul wrote:
The code looks like the following:
-----------------------
C::C(const char* ss...)
try
: ..., settings(ss) /*third-party class that can throw E*/, ...
{
...
}
catch (const E& e) {
std::cerr << e.what();
The compiler inserts here:
throw;
}
;
-----------------------
(First, a minor point: in my experience whenever constructs similar to above are used - when "try" appears outside the main function body not necessarily within a constructor (can be something along these lines: int main() try {...} catch (...) {...}) - unless they are fairly simple, code will not compile without the semicolon (;) after the closing curly brace.)
I understand that when a constructor of a class throws an exception, that class will not be constructed even if the exception is caught as in the above code - and I do not count on the object creation.
The exception is automatically rethrown at the end of the catch block for a function try block of a constructor or destructor.
However, I could not
find a way to exit the application gracefully in this case.
Just catch the exception (assuming the object being constructed isn't global).
The above works
to the extent that the error (exception) is reported properly but this is followed by
-----------------------
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
-----------------------
and then the dialogue box "Please tell Microsoft about this problem." appears.
When the object construction is put within a try-block
try { C c(...); } catch (...) { std::cerr << "Error.\n"; }
the application just silently exits.
Presumably with e.what() and "Error.\n" output, right?
Is there a way to display just the error message when "settings(ss)" throws an exception without the "This application has requested..."? (I am aware of how to disable "Please tell Microsoft..." for an application via an API but it is "The application has requested..." that I do not know how to deal with.)
It's up to you how you handle the exception, but if you let an exception propogate up through main as you do in your first version (where presumably the object creation isn't in a try block) I think that dialog is unavoidable.
Tom
.
- Follow-Ups:
- References:
- Prev by Date: Re: memory allocation problem
- Next by Date: Re: Using std::cin.rdbuf()->in_avail()
- 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
|