繁体   English   中英

如何从C ++ catch(...)块获取错误消息?

[英]How to get the error message from a C++ catch(…) block?

所以,我正在查看try / catch块的C ++参考

我看到有几种方法可以捕获异常,如下所示:

try {
    f();
} catch (const std::overflow_error& e) {
    // this executes if f() throws std::overflow_error (same type rule)
} catch (const std::runtime_error& e) {
    // this executes if f() throws std::underflow_error (base class rule)
} catch (const std::exception& e) {
    // this executes if f() throws std::logic_error (base class rule)
} catch (...) {
    // this executes if f() throws std::string or int or any other unrelated type
}

我在以下示例中看到您可以捕获“e”数据,如下所示:

std::cout << e.what();

所以我的问题归结为:

如何获取catch(...)上的异常数据?

(旁边的问题:使用catch(...)是否明智?)

一般来说,你不能。 C ++几乎可以抛出任何东西。 例如, throw 42; 是完全定义良好的C ++代码,异常的类型是int

至于使用它是明智的 - 有效用途:

  • 如果抛出异常并且没有一直有catch块,则调用std::terminate并且不能保证堆栈展开。 catch(...)保证(因为它捕获任何异常)。

int main()
{
    super_important_resource r;
    may_throw();
    // r's destructor is *not* guaranteed to execute if an exception is thrown
}

int main()
try {
    super_important_resource r;
    may_throw();
    // r's destructor is guaranteed to execute during stack unwinding
} catch(...) {
}
  • 在重新抛出异常之前,记录异常被抛出是一个有效的用例。

try {
//...
} catch(...) {
    log() << "Unknown exception!";
    throw;
}

如何获取catch(...)上的异常数据?

一般情况下,您无法获得任意异常。 但是,如果异常类型是已知类型之一,则可以重新抛出当前异常并捕获它。

catch(...)问题:使用catch(...)是否明智?)

使用at作为后备选项来处理意外异常是有意义的。 人们可以考虑捕获重新捕获技术,以避免在几个地方捕获系列的复制粘贴。

void Catcher()
{
    try
    {
        throw;
    }
    catch (const std::overflow_error& e) {
        // this executes if f() throws std::overflow_error (same type rule)
    }
    catch (const std::runtime_error& e) {
        // this executes if f() throws std::underflow_error (base class rule)
    }
    catch (const std::exception& e) {
        // this executes if f() throws std::logic_error (base class rule)
    }
    catch (...) {
        // oops!
    }
}

int main()
{
    try {
        f();
    }
    catch (...) {
        Catcher();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM