gpt4 book ai didi

c++ - C++ catch(std::exception&e)与catch(…)

转载 作者:行者123 更新时间:2023-12-02 10:28:18 29 4
gpt4 key购买 nike

我知道这两种捕获方法在处理上的区别,但是椭圆捕获std::exception catch不会捕获到什么东西呢?

例如:

try
{
throw std::runtime("runtime error!");
}
catch(const std::exception& e)
{
std::cout << "Exception: " << e;
}
catch(...)
{
std::cout << "How did I get here?";
throw;
}

我已经看到了将两者结合使用的代码示例,但没有看到您同时使用这两个代码的原因。

最佳答案

catch(const std::exception& e)

仅捕获标准异常。
catch(...)

之后将捕获所有内容。

您可以处理整数和其他类型( http://www.cplusplus.com/doc/tutorial/exceptions/)

例如:
catch(int e)

关于c++ - C++ catch(std::exception&e)与catch(…),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63355989/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com