Let the C + + program gracefully crash

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.