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

Tech-Archive recommends: Speed Up your PC by fixing your registry



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
.



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)