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):
#include <iostream>
#include <fstream>
#include <string>
#include <new>
/* load_file(filepath, destination)
-Returns whether it succeed or not. True meaning succeeded.
-Takes a path and writes to a std::string, completely memory safe.
*/
bool load_file(const char *filepath, std::string &dest)
throw (std::bad_alloc) {
using namespace std;
ifstream file;
file.open(filepath, ios::binary);
if (!file.good()) {
cerr << "Fatal error while opening file." << endl;
return false;
}
//get filesize in bytes from ifstream
//seek end, get pointer, rewind
file.seekg(0, ios_base::end);
size_t file_size = file.tellg();
file.seekg(0, ios_base::beg);
if (!file.good()) {
cerr << "Fatal error while getting filesize." << endl;
return false;
}
//read file in string from ifstream
//allocate array, read file in array, set the term. null byte, set string to array
char *file_content;
try {
file_content = new char [file_size+1];
} catch (bad_alloc &memException) {
cerr << "Probably went out of memory while allocating " << file_size+1 << " bytes!" << endl;
file.close();
throw;
}
file.read(file_content, file_size);
file_content[file_size] = '\0';
dest = file_content;
//clean up
//close file, free array
file.close();
delete[] file_content;
return true;
}
void custom_terminate() {
std::cerr << "--Aye, unrecoverable exception thrown!\n"
<< "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;
std::abort();
}
int main() {
using namespace std;
set_terminate(custom_terminate);
string file_content;
load_file("C:\\Program Files (x86)\\SEGA\\Condemned - Criminal Origins\\Game\\CondemnedA.Arch00", file_content);
return 0;
}
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:
Probably went out of memory while allocating 2384200440 bytes!
--Aye, unrecoverable exception thrown!
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.
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
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! :D