捕获所有未处理的C++异常?

Fir*_*cer 24 c++ exception-handling exception

有没有办法捕获原本未处理的异常(包括那些在catch块之外抛出的异常)?

我并不是真的关心所有正常的清理工作,只是因为我可以捕获它,将其写入日志/通知用户并退出程序,因为这些情况下的例外通常是致命的,不可恢复的错误.

就像是:

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);
}
Run Code Online (Sandbox Code Playgroud)

Evi*_*ach 26

这可用于捕获意外的异常.

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

没有try catch块,我认为你不能捕获异常,所以构造你的程序,所以异常的代码是在try/catch的控制下.

  • 性能不是问题,只有当实际抛出异常时,系统才必须考虑要做什么.无论如何,当对象超出范围时,堆栈展开是应用程序必须执行的操作.另外,尝试一下 - 它很容易把try/catch放在main中,看看是否有性能损失. (8认同)
  • main中的全局捕获(...)仍然不会捕获全局变量中的异常.例如,全局std :: vector <int>(size_t(-1))将在调用main()之前耗尽内存. (5认同)
  • 除非抛出异常,否则在现代编译器中使用异常几乎没有成本.此时它与使用任何其他方法查找和报告错误一样昂贵. (3认同)
  • @msalters一种解决问题的技巧是制作你的全局指针,然后在主要指针中分配它们. (3认同)

kra*_*lyk 20

查看 std::set_terminate()

  • 是的,您可以使用std :: uncaugth_exception()和std :: current_exception {) (2认同)
  • @Sz。您可以使用 `try { std::rethrow_exception(the_pointer); } catch(const YourException&amp; e) { ... }` (2认同)

gbj*_*anb 11

您可以在Windows上使用SetUnhandledExceptionFilter,它将捕获所有未处理的SEH异常.

通常,这对于您的所有问题都已足够,因为IIRC将所有C++异常实现为SEH.


小智 8

没有任何catch块,您将不会遇到任何异常.你可以在main()中使用catch(...)块(以及每个附加线程中的等效块).在此catch块中,您可以恢复异常详细信息,并且可以对它们执行某些操作,例如记录和退出.

但是,一般的catch(...)块也存在缺点:系统发现异常已由您处理,因此它不再提供任何帮助.在Unix/Linux上,此帮助将构成一个CORE文件,您可以将其加载到调试器中并查看未触发异常的原始位置.如果你用catch(...)处理它,这些信息就会丢失.

在Windows上,没有CORE文件,所以我建议使用catch(...)块.从该块开始,您通常会调用一个函数来恢复实际的异常:

std::string ResurrectException()
   try {
       throw;
   } catch (const std::exception& e) {
       return e.what();
   } catch (your_custom_exception_type& e) {
       return e.ToString();
   } catch(...) {
       return "Ünknown exception!";
   }
}


int main() {
   try {
       // your code here
   } catch(...) {
       std::string message = ResurrectException();
       std::cerr << "Fatal exception: " << message << "\n";
   }
}
Run Code Online (Sandbox Code Playgroud)

  • Windows有.dmp文件,大致相当于核心文件,但它们会上传到Windows错误报告网站(如果用户点击"发送"),而不是乱丢用户的硬盘.此外,如果您配置了即时调试器,Windows将会进入调试器. (2认同)

pax*_*977 7

更新:这仅涵盖c ++ 98.

从Meyers的更有效的C++(第76页)中,您可以定义一个函数,当函数生成未由其异常规范定义的异常时,该函数被调用.

void convertUnexpected()
{
    // You could redefine the exception here into a known exception
    // throw UnexpectedException();

    // ... or I suppose you could log an error and exit.
}
Run Code Online (Sandbox Code Playgroud)

在您的应用程序中注册功能:

std::set_unexpected( convertUnexpected );
Run Code Online (Sandbox Code Playgroud)

如果函数生成一个未由其异常规范定义的异常,则会调用您的函数convertUnexpected()...这意味着只有在使用异常规范时这才有效.(

  • 我知道这个答案早在c ++ 11标准可用之前就已经提出,但目前`std :: set_unexpected`已被弃用. (3认同)

Mar*_*ork 6

这就是我在 main() 中经常做的

int main()
{
    try
    {
        // Do Work
    }
    catch(std::exception const& e)
    {
         Log(e.what());
         // If you are feeling mad (not in main) you could rethrow! 
    }
    catch(...)
    {
         Log("UNKNOWN EXCEPTION");
         // If you are feeling mad (not in main) you could rethrow! 
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这是一个很好的方法,但应该记住,这不会捕获可能的静态初始化异常(我相信)。 (2认同)
  • 我知道...从技术上讲,可能可以使用 set_terminate() (请参阅我的答案),但由于静态初始化顺序是实现定义的,所以也不能保证... (2认同)

scr*_*ari 5

如果 C++11 可用,则可以使用此方法(参见示例:http : //en.cppreference.com/w/cpp/error/rethrow_exception):

#include <iostream>
#include <exception>

void onterminate() {
  try {
    auto unknown = std::current_exception();
    if (unknown) {
      std::rethrow_exception(unknown);
    } else {
      std::cerr << "normal termination" << std::endl;
    }
  } catch (const std::exception& e) { // for proper `std::` exceptions
    std::cerr << "unexpected exception: " << e.what() << std::endl;
  } catch (...) { // last resort for things like `throw 1;`
    std::cerr << "unknown exception" << std::endl;
  }
}

int main () {
  std::set_terminate(onterminate); // set custom terminate handler
  // code which may throw...
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这种方法还允许您为未处理的异常自定义控制台输出:有这样的东西

unexpected exception: wrong input parameters
Aborted
Run Code Online (Sandbox Code Playgroud)

而不是这个:

terminate called after throwing an instance of 'std::logic_error'
  what():  wrong input parameters
Aborted
Run Code Online (Sandbox Code Playgroud)