Can you really have a function/method without a body but just a try/catch block?(你真的可以有一个没有主体的函数/方法,而只是一个 try/catch 块吗?)
问题描述
请注意,此函数没有{"和}"主体.只是一个 try/catch 块:
Note that this function does not have a "{" and "}" body. Just a try/catch block:
void func( void )
try
{
...
}
catch(...)
{
...
}
这是有意成为 C++ 的一部分,还是 g++ 扩展?
Is this intentionally part of C++, or is this a g++ extension?
除了绕过 1 级 {} 之外,还有其他目的吗?
Is there any purpose to this other than bypass 1 level of {}?
在遇到 http://stupefydeveloper.blogspot.com/2008/10/c-function-try-catch-block.html
推荐答案
是的,这是标准的.函数 try 块,因为它们被称为,对常规函数没有太多用处,但对于构造函数,它们允许您捕获初始化器列表中抛出的异常.
Yes, it is standard. Function try blocks, as they're called, aren't that much use for regular functions, but for constructors, they allow you to catch exceptions thrown in the initialiser list.
请注意,在构造函数的情况下,异常总是会在任何 catch 块的末尾重新抛出.
Note that, in the constructor case, the exception will always be rethrown at the end of any catch blocks.
这篇关于你真的可以有一个没有主体的函数/方法,而只是一个 try/catch 块吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:你真的可以有一个没有主体的函数/方法,而只是
基础教程推荐
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 在 C++ 中计算滚动/移动平均值 2021-01-01
