Nar*_*ode 6 c++ exception throw
如果这些信息是否正确,任何人都可以确认我:
在C++中,在catch块中我们可以使用throw语句重新抛出异常,但抛出的异常应该与当前捕获的异常具有相同的类型.
dlf*_*dlf 12
throw;所有在catch块中重新抛出刚刚捕获的异常.如果您需要(例如)执行一些清理操作以响应异常,这仍然很有用,但仍然允许它将upstack传播到可以更全面地处理的地方:
catch(...)
{
cleanup();
throw;
}
Run Code Online (Sandbox Code Playgroud)
但你完全可以自由地做到这一点:
catch(SomeException e)
{
cleanup();
throw SomeOtherException();
}
Run Code Online (Sandbox Code Playgroud)
实际上,为了将您调用的代码抛出的异常转换为您抛出的文档类型,通常很方便.
重新抛出的异常可以有不同的类型。这在 VS2012 上编译并正确运行:
#include <iostream>
int main() try
{
try
{
throw 20;
}
catch (int e)
{
std::cout << "An exception occurred. Exception Nr. " << e << std::endl;
throw std::string("abc");
}
}
catch (std::string const & ex)
{
std::cout << "Rethrow different type (string): " << ex << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
输出:
An exception occurred. Exception Nr. 20
Rethrow different type (string): abc
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8437 次 |
| 最近记录: |