开发者

C++ unhandled exceptions

开发者 https://www.devze.com 2023-01-17 18:10 出处:网络
Does C++ offer a way to \'show\' something visual if an unhandled exception occurs? What I want to do is to make something like assert(unhandled exception.msg()) if it actually happens (like in the fo

Does C++ offer a way to 'show' something visual if an unhandled exception occurs?

What I want to do is to make something like assert(unhandled exception.msg()) if it actually happens (like in the following sample):

#inc开发者_如何学运维lude <stdexcept>

void foo() {
   throw std::runtime_error("Message!");
}

int main() {
 foo();
}

I expect this kind of code not to terminate immediately (because exception was unhandled), rather show custom assertion message (Message! actually).

Is that possible?


There's no way specified by the standard to actually display the message of the uncaught exception. However, on many platforms, it is possible anyway. On Windows, you can use SetUnhandledExceptionFilter and pull out the C++ exception information. With g++ (appropriate versions of anyway), the terminate handler can access the uncaught exception with code like:

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

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

IN short, no there's no generic C++ way, but there are ways depending on your platform.


Microsoft Visual C++ allows you to hook unhandled C++ exceptions like this. This is standard STL behaviour.

You set a handler via a call to set_terminate. It's recommended that your handler do not very much work, and then terminate the program, but I don't see why you could not signal something via an assert - though you don't have access to the exception that caused the problem.


I think you would benefit from a catch-all statement as follows:

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


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


If I'm reading your question correctly, you're asking if you can overload throw (changing its default behavior) so it does something user-defined. No, you can't.

Edit: since you're insistent :), 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's obviously platform-dependent, but it works.


Yes, its possible. Here you go:

#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;
}


The c++ standard is the terminate handler - as other have said

If you are after better traceablility for throws then this is what we do

We have a macro Throw that logs the file name and line number and message and then throws. It takes a 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


If you're really interested in what happened to cause your program to fail, you might benefit from examining the process image in a post-mortem debugger. The precise technique varies a bit from OS to OS, but the basic train is to first enable core dumping, and compile your program with debug symbols on. Once the program crashes, the operating system will copy its memory to disk, and you can then examine the state of the program at the time it crashed.

0

精彩评论

暂无评论...
验证码 换一张
取 消