如何有条件地捕获异常

How to catch exceptions conditionally?

本文关键字:捕获异常 有条件      更新时间:2023-10-16

我的大型应用程序具有以下结构:

int main()
{
    try {
        ...
    } catch (std::exception& e) {
        std::cout << "Fatal error: " << e.what() << (some more data) << std::endl;
        return 1;
    }
}

在调用堆栈的深处,各种对象检查其内部状态,如果检测到错误,则抛出std::runtime_exception。包罗万象的异常处理程序捕获它,打印一些中等有用的信息并终止程序。

但是,当我在MS Visual Studio下进行调试时,我可以从没有任何异常处理程序中受益:Visual Studio有自己的非常有用的处理程序,它可以在引发异常的位置停止我的应用程序,因此我可以检查出了什么问题。

如何有条件地捕获异常?

我尝试了以下方法:

    try {
        ...
    } catch (std::exception& e) {
        if (IsDebuggerPresent())
            throw;
        else
            std::cout << "Fatal error: " << e.what() << (some more data) << std::endl;
    }

这给出了一个奇怪的结果:Visual Studio 捕获了重新引发的异常,并向我展示了引发异常的点的堆栈跟踪。但是,我的应用程序中的所有对象显然都被破坏了,我看不到例如局部或成员变量。

我可以使异常处理程序以编译标志为条件:

#ifdef NDEBUG
    try {
#endif
        ...
#ifdef NDEBUG
    } catch (std::exception& e) {
        std::cout << "Fatal error: " << e.what() << (some more data) << std::endl;
    }
#endif

但这很不方便,因为如果我想调试它,我必须重新编译所有内容。

那么,如何使异常处理成为条件(例如,取决于命令行参数)?

那么,如何使异常处理成为条件(例如,取决于命令行参数)?

通过为它编写代码:o]

请考虑以下原始代码:

int main()
{
    try {
        run_the_application(); // this part different than your example
    } catch (std::exception& e) {
        std::cout << "Fatal error: " << e.what() << (some more data) << std::endl;
        return 1;
    }
}

新代码:

template<typename F>
int fast_run(F functor) { functor(); return EXIT_SUCCESS; }
template<typename F>
int safe_run(F functor)
{
    try {
        functor();
    } catch (std::exception& e) {
        std::cout << "Fatal error: " << e.what() << (some more data) << std::endl;
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}
template<typename F>
int run(const std::vector<std::string>& args, F functor)
{
    using namespace std;
    if(end(args) != find(begin(args), end(args), "/d"))
        return fast_run(functor);
    else
        return safe_run(functor);
}
int main(int argc, char** argv)
{
    const std::vector<std::string> args{ argv, argv + argc };
    return run(args, run_the_application);
}

正如CompuChip所建议的那样,Visual Studio可以在抛出异常时中断执行,而不仅仅是在捕获未捕获的异常时!

要启用此功能(在 Visual Studio 2012 中):

  1. 在菜单中,转到调试 ->异常
  2. 在打开的窗口中,勾选所有C++异常的"抛出"框(仅勾选std::exception是不够的 - 我不知道为什么)
  3. 运行您的程序

这远非很好,但您可以在 CMake 中定义命令行参数(假设您使用它):

-DCATCH_ALL=true

然后,在 CMakelists.txt 中,您可以将其传播到C++宏:

if (CATCH_ALL)
  message("Some exceptions with not be caught")
  add_compile_definitions(CATCH_ALL)
else ()
  message("Trying to catch more exceptions to provide better diagnostics")
endif ()

最后,在代码中:

#ifdef CATCH_ALL
try {
#endif
  a = big_problematic_algorithm(problematic_parameter);
#ifdef CATCH_ALL
 } catch (const std::exception &err) {
  LOGF(WARN, "Crashed for the parameter %s: %s", problematic_parameter.c_str(), err.what());
 }
#endif

它相当笨拙,但如果不经常使用,还可以,只能在非常高的某个地方(处理完整的Web请求,用户操作,要处理的文件等)。如果在 IDE 中运行时未发生崩溃,它允许提供更好的诊断日志,如果您自己抛出此异常,也可以恢复,因此知道如何操作。

可以将 IDE 配置为设置 CMake 参数。在集成服务器上或从命令行构建时,激活异常的额外处理不会完成此操作。