学习"尝试和捕捉".以下代码有什么问题?感谢您的建议.
执行错误:
terminate called without an active exception
Aborted
Run Code Online (Sandbox Code Playgroud)
代码:
#include <stdio.h>
int main()
{
int a = 3;
try
{
if (a < 5)
throw;
}
catch (...)
{
printf ("captured\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
您的throw;语句尝试重新抛出当前异常,但可能没有.你需要类似的东西
throw some_exception_object();
Run Code Online (Sandbox Code Playgroud)
在块内部try,您必须指定要抛出的内容。您可以单独使用的唯一位置throw是在块内catch重新抛出当前异常。如果您throw在没有当前异常处于活动状态的情况下自行调用,您将杀死您的应用程序,正如您已经发现的那样。
尝试这个:
#include <stdio.h>
int main()
{
int a = 3;
try
{
if (a < 5)
throw 1; // throws an int
}
catch (...)
{
printf ("captured\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
throw只要你扔东西,你就可以做任何你想要的事情。
| 归档时间: |
|
| 查看次数: |
1800 次 |
| 最近记录: |