Karachi   ->   Sweden   ->   Karachi, again   ->   Dubai   ->   Bahrain   ->   Karachi, once more   ->   London and Leeds

Tuesday, May 27, 2008

Disabling The Application Crash Dialog Box in C++

First of all, you must know that there are two types of exceptions that can arise in a C++ program: synchronous and asynchronous. The names might be misleading but the concept is simple: A synchronous exception is what is actually "thrown" in code. An asynchronous is a hardware or Windows level exception. For example, you might get "bad_alloc" exception thrown at you when insertion in an stl vector fails. That would be a synchronous exception. An example of asynchronous exception is division by zero. There is no "throw" statement which actually causes the exception to be thrown.

When you have a catch all block, written as catch (...) in your code, asynchronous exceptions might or might not get caught, depending on
  • whether you are compiling in debug mode or release mode

  • whether you are using Visual Studio 2005 or an earlier version

If you want to go into the details of what has been explained above, this could be a very good article.

Coming back to the original topic, when you have an unhandled exception, i.e. something for which there is no appropriate catch block, several things happen in the background. You can read this MSJ article for details. In short, your application crashes (but doesn't terminate!), and you get a popup at the end similar to the one shown below. Once you press OK on this popup, the process terminates completely.

You can disable the release mode pop-up using
SetErrorMode (SEM_NOGPFAULTERRORBOX);

However, if you are building in debug mode, the pop-up box would be completely different; offering debug services!

The reason you see this "application has requested the runtime to terminate it" is because of the settings debugger does. You can disable them by using _CrtSetReportMode and _CrtSetReportFile methods. You'll have to include crtdbg.h in your code.
Below is one example:
_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE);
_CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDERR );