Corrected C ++ Exceptions

Does C ++ provide a way to β€œshow” something visual if an unhandled exception occurs?

I want to do something like assert(unhandled exception.msg()) if this really happens (for example, in the following example):

 void foo() { throw std::exception("Message!"); } int main() { foo(); } 

I expect that such code will not be immediately terminated (as the exception was unhandled), rather show a custom approval message ( Message! In fact).

Is it possible?

+6
c ++ handler exception
source share
8 answers

The standard does not specify how to display an uncaught exception message. However, on many platforms this is possible. On Windows, you can use SetUnhandledExceptionFilter and pull out C ++ exception information. With g ++ (corresponding versions anyway), the terminate handler can access an uncaught exception with code, for example:

  void terminate_handler() { try { throw; } catch(const std::exception& e) { log(e.what()); } catch(...) {} } 

and indeed, the g ++ default terminate handler does something similar to this. You can set the completion handler with set_terminate.

In short, there is no general C ++ way, but there are ways that depend on your platform.

+9
source share

Microsoft Visual C ++ allows you to attach raw C ++ exceptions to standard STL behavior .

You set the handler to call set_terminate . He recommended that your handler not work very hard, and then exit the program, but I don’t understand why you could not signal anything through assert - although you do not have access to the exception that caused the problem.

+6
source share

If you use Windows, CrashRpt is a good library for handling unhandled exceptions and crashes. If you want to do this manually, you can also use the following, which I wrote in this answer .

+4
source share

I think you will benefit from the catch-all statement as follows:

 int main() { try { foo(); catch (...) { // Do something with the unhandled exception. } } 
+4
source share

If I read your question correctly, you ask if you can overload throw (by changing its default behavior) so that it does something user-defined. No, you can’t.

Edit: since you are persistent :), here's a Bad Idea β„’:

 #include <iostream> #include <stdlib.h> #include <windows.h> void monkey() { throw std::exception("poop!"); } LONG WINAPI MyUnhandledExceptionFilter(struct _EXCEPTION_POINTERS *lpTopLevelExceptionFilter) { std::cout << "poop was thrown!" << std::endl; return EXCEPTION_EXECUTE_HANDLER; } int main() { SetUnhandledExceptionFilter(&MyUnhandledExceptionFilter); monkey(); return 1; } 

Again, this is a very bad idea, and it clearly depends on the platform, but it works.

+1
source share

Yes it is possible. Here you are:

 #include <iostream> #include <exception> void foo() { throw std::exception("Message!"); } int main() { try { foo(); } catch (std::exception& e) { std::cout << "Got exception: " << e.what() << std::endl; } return 0; } 
+1
source share

C ++ standard is a terminate handler - as others said

If you are after better throw tracking, then this is what we do.

We have a Throw macro that registers the file name, line number and message, and then throws. It accepts printf style varargs message.

 Throw(proj::FooException, "Fingle %s unable to process bar %d", fingle.c_str(), barNo); 

I get a nice log message

 Throw FooException from nargle.cpp:42 Fingle barf is unable to process bar 99 
0
source share

If you are really interested in what caused your program to crash, you may need to examine the process image in the post-mortem debugger. The exact method varies slightly from OS to OS, but the main train must first enable a kernel reset, and compile your program using debugging symbols. As soon as the program crashes, the operating system will copy its memory to disk, and you can check the status of the program at the time of its failure.

0
source share

All Articles