13

I'm working on the development of a software in C++ on Visual Studio 2010. As this software should be run on servers where human interaction is not available, I really really need to get rid of this "program.exe has stopped working" window that pops up in the release version in case of errors. I just want the program to terminate (maybe also give an error message, but not necessarily) and not have it remain blocked waiting for someone to click the "Close the program" button. I have to mention that I have 64 bit Windows 7 Professional on my machine.

I have read about several things, such as:

  • the _set_abort_behavior function. This solves the case when abort() is called, but that is not the case for errors like "vector subscript out of range".

  • I know I could be solving some of these errors by doing exception handling, but not all errors are exceptions, therefore this would not solve my entire problem.

  • I've also read something about the Dr. Watson debugger, which is supposed to terminate the application silently, but I have the impression that this debugger is not available for Windows 7. Plus I don't know if this debugger would solve my problem in the release mode...

  • I don't find that disabling Error Reporting on my entire machine is an elegant option, although I read that this could also be an alternative (not really one that I want to take).

How can I do this in Visual Studio? Are there any settings I could use?

Is there maybe a way to turn all errors in exceptions in Visual Studio, so that I can solve the problem with the exception handling mechanism? (please be tolerant if this was a stupid question)

I'm looking forward to your proposals. Many thanks for your time!

Best regards, Cornelia

Cornelia Serban
  • 133
  • 1
  • 1
  • 4
  • 2
    The best way would be to get rid of whatever's causing the crash in the first place. – chris Jul 19 '12 at 11:02
  • "I've also read something about the Dr. Watson debugger, which is supposed to terminate the application silently" - This is kind of "exceptional" usage for debugger (of which i never heard of, debugger can be automatically started in case of application crash). And certainly not for release mode. – SChepurin Jul 19 '12 at 11:24
  • @Chris: that's an obvious answer! But the actual problem is that when the application is running on some server somewhere and it encounters an error and crashes, it remains hanging and I don't know if it is actually still running or stuck (without implementing some kind of platform dependent watcher on the backend system, which is not what I want!). So I'd rather the application closes after encoutering the error and afterwards I can check the log file and see that it unexpectedly stopped working and so I will know that I have a problem. – Cornelia Serban Jul 19 '12 at 15:13
  • @SChepurin: I assumed it would be like that. Thanks for your reply! – Cornelia Serban Jul 19 '12 at 15:13
  • If your application is running without human supervision, it should really be a system service, in which case you wouldn't have this problem. – Harry Johnston Jul 19 '12 at 23:14

3 Answers3

19

You can use

SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);

MSDN documentation for SetErrorMode

You can also refer here

Hope this will solve your problem...

Community
  • 1
  • 1
jsist
  • 5,223
  • 3
  • 28
  • 43
1

A good way is to add your own unhandled exception filter (see SetUnhandledExceptionFilter() docs) and then write a log and a mini dump so that you can do whatever error handling you need to (eg close devices/handles) and you also have a crash dump that you can analyse later. Useful information in this answer: https://stackoverflow.com/a/1547251/188414

Community
  • 1
  • 1
the_mandrill
  • 29,792
  • 6
  • 64
  • 93
  • Thank you for your reply. The previous solution solved my problem. But just for my curiosity, this SetUnhandledExceptionFilter(), does it work for all errors or just exceptions? The problem is that in C++ not all errors are exceptions (like in Java for example), for example errors that come from an undefined behavior are not exceptions. – Cornelia Serban Jul 19 '12 at 15:07
  • In native C++ on Windows there are two types of exceptions -- the regular C++ exceptions but also Structured Exceptions which are specific to windows. These can catch runtime errors such as pure virtual function calls and access violations which you can't catch in regular C++. This gives your application a chance to tidy up in the case of a fatal error. See more in MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/ms680657%28v=vs.85%29.aspx – the_mandrill Jul 19 '12 at 16:00
-1

Sometimes this error occurs only because you have not added a & sign before the value you have used in scanf

Try the following which solved my problem

From

scanf("%d",code);

To

scanf("%d",&code);
Starx
  • 77,474
  • 47
  • 185
  • 261