Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching all unhandled C++ exceptions?

Is there some way to catch exceptions which are otherwise unhandled (including those thrown outside the catch block)?

I'm not really concerned about all the normal cleanup stuff done with exceptions, just that I can catch it, write it to log/notify the user and exit the program, since the exceptions in these casese are generaly fatal, unrecoverable errors.

something like:

global_catch() {     MessageBox(NULL,L"Fatal Error", L"A fatal error has occured. Sorry for any inconvience", MB_ICONERROR);     exit(-1); } global_catch(Exception *except) {     MessageBox(NULL,L"Fatal Error", except->ToString(), MB_ICONERROR);     exit(-1); } 
like image 836
Fire Lancer Avatar asked Nov 09 '08 16:11

Fire Lancer


People also ask

Where do I find unhandled exceptions?

If your application has unhandled exceptions, that may be logged in the Windows Event Viewer under the category of “Application”. This can be helpful if you can't figure out why your application suddenly crashes. Windows Event Viewer may log 2 different entries for the same exception.

How do I catch all exceptions in CPP?

Exception handling is used to handle the exceptions. We can use try catch block to protect the code. Catch block is used to catch all types of exception. The keyword “catch” is used to catch exceptions.

Does exception catch all exceptions?

Since Exception is the base class of all exceptions, it will catch any exception.


1 Answers

This can be used to catch unexpected exceptions.

catch (...) {     std::cout << "OMG! an unexpected exception has been caught" << std::endl; } 

Without a try catch block, I don't think you can catch exceptions, so structure your program so the exception thowing code is under the control of a try/catch.

like image 136
EvilTeach Avatar answered Oct 01 '22 01:10

EvilTeach