Windows' Default Exception Message

Thread Solved

Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Windows' Default Exception Message

 
0
  #1
Apr 14th, 2009
Hey guys,

How do I disable/get rid of that default exception message windows shows when you have an "unhandled" exception in your program?

Here's my example code (sorry for not limiting the code's size to the problem only, it's readable enough I think):
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <new>
  5.  
  6. /* load_file(filepath, destination)
  7.  
  8.   -Returns whether it succeed or not. True meaning succeeded.
  9.   -Takes a path and writes to a std::string, completely memory safe.
  10. */
  11.  
  12. bool load_file(const char *filepath, std::string &dest)
  13. throw (std::bad_alloc) {
  14. using namespace std;
  15.  
  16. ifstream file;
  17. file.open(filepath, ios::binary);
  18. if (!file.good()) {
  19. cerr << "Fatal error while opening file." << endl;
  20. return false;
  21. }
  22.  
  23. //get filesize in bytes from ifstream
  24. //seek end, get pointer, rewind
  25. file.seekg(0, ios_base::end);
  26. size_t file_size = file.tellg();
  27. file.seekg(0, ios_base::beg);
  28. if (!file.good()) {
  29. cerr << "Fatal error while getting filesize." << endl;
  30. return false;
  31. }
  32.  
  33. //read file in string from ifstream
  34. //allocate array, read file in array, set the term. null byte, set string to array
  35.  
  36. char *file_content;
  37. try {
  38. file_content = new char [file_size+1];
  39. } catch (bad_alloc &memException) {
  40. cerr << "Probably went out of memory while allocating " << file_size+1 << " bytes!" << endl;
  41. file.close();
  42. throw;
  43. }
  44.  
  45. file.read(file_content, file_size);
  46. file_content[file_size] = '\0';
  47. dest = file_content;
  48.  
  49. //clean up
  50. //close file, free array
  51. file.close();
  52. delete[] file_content;
  53.  
  54. return true;
  55. }
  56.  
  57. void custom_terminate() {
  58. std::cerr << "--Aye, unrecoverable exception thrown!\n"
  59. << "Try doing what the messages say (if anything) and run the program again or ask someone with a bit more expertise to help you out." << std::endl;
  60. std::abort();
  61. }
  62.  
  63.  
  64. int main() {
  65. using namespace std;
  66.  
  67. set_terminate(custom_terminate);
  68.  
  69. string file_content;
  70. load_file("C:\\Program Files (x86)\\SEGA\\Condemned - Criminal Origins\\Game\\CondemnedA.Arch00", file_content);
  71.  
  72. return 0;
  73. }

So, the exception gets thrown at line 40, handles it there nicely. Then it rethrows it to main, which in term calls (my own) terminate() which is also good, but then windows' message still pops up:

  1. Probably went out of memory while allocating 2384200440 bytes!
  2. --Aye, unrecoverable exception thrown!
  3. Try doing what the messages say (if anything) and run the program again or ask someone with a bit more expertise to help you out.
  4.  
  5. This application has requested the Runtime to terminate it in an unusual way.
  6. Please contact the application's support team for more information.
  7.  
I want to get rid of those last 2 lines, my programs will not have a support team.

Thanks in advance,
Nick

PS: Obviously, you should set the filepath in main to some big file that you're PC can't handle, or just alter the new[] (in load_file()) to have some big big big number. You might want to disable your swap before you do this though!
Last edited by Clockowl; Apr 14th, 2009 at 11:51 am.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Windows' Default Exception Message

 
0
  #2
Apr 14th, 2009
Yeey self reply!

Calling abort() shows that message, exit() does not. Thanks Nick! You're welcome!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 559 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC