你真的有一个函数/方法没有一个身体,但只是一个try / catch块? [英] Can you really have a function/method without a body but just a try/catch block?

查看:99
本文介绍了你真的有一个函数/方法没有一个身体,但只是一个try / catch块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,此函数没有{和}正文。只是尝试/ 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级{}?

我从来没有听说过,直到我遇到了 http://stupefydeveloper.blogspot.com/2008/10/c-function-try-catch-block.html

I'd never heard of this until I ran into 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块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆