Program crashes are a headache for every C + + programmer. You may have used a null pointer, possibly an array out of bounds. Anyway, in some messy situations, the program will flash back, or pop up a dialog box similar to the following is unacceptable situation.
To make our program die less ugly, Windows provides a function like this:
Lptop_level_exception_filter
WINAPI
SetUnhandledExceptionFilter (
__in_opt Lptop_level_exception_filter Lptoplevelexceptionfilter
);
By MSDN we can know that when any exception is sent in the current process, SetUnhandledExceptionFilter is able to capture it and will call the Lptoplevelexceptionfilter callback function.
So when the exception is sent we can do what we want to do in Lptoplevelexceptionfilter.
LONG Callbackcrashhandler (exception_pointers *pexception) {// Here you can make a nice interface or other// MessageBox (null,l " Oops mom, collapsed ", L" error ", MB_OK); return exception_execute_handler; } void Crash () { int i =; int j = 0; int m = i/j; printf ("%d", m);} int _tmain (int argc, _tchar* argv[]) { //Set callback function unhandled for handling exception SetUnhandledExceptionFilter (( Lptop_level_exception_filter) callbackcrashhandler); Crash (); return 0; }
Let the C + + program gracefully crash