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 块吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:你真的可以有一个没有主体的函数/方法,而只是


基础教程推荐
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- C++,'if' 表达式中的变量声明 2021-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 设计字符串本地化的最佳方法 2022-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01