函数的try-catch语法之间的区别 [英] Difference between try-catch syntax for function

查看:230
本文介绍了函数的try-catch语法之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了 try-catch 的语法。

struct A
{
  int a;

  A (int i) : a(i)  // normal syntax
  {
    try {}
    catch(...) {}
  }

  A ()   // something different
  try : a(0) {}
  catch(...) {}

  void foo ()  // normal function
  try {}
  catch(...) {}
};

这两种语法有效。这些语法和编码风格有什么技术上的差别吗?

Both syntax are valid. Is there any technical difference between these syntax apart from coding style ? Is one of the syntax superior to other by any aspect ?

推荐答案

第一个语法:

try块的范围在成员初始化列表完成后开始,因此在成员初始化期间抛出的任何异常都不会被此try-catch块捕获。

The First Syntax:
The scope of the try block starts after the Member Initialization list has been completed, So any exception thrown during Member Initialization will not be caught by this try-catch block.

第二种语法:

它确保如果在成员初始化列表中抛出异常,那么您可以捕获异常。

The second syntax:
It ensures that if an exception gets thrown during Member Initialization list then you are able to catch the exception.

第三个语法:

它确保从try块的起始大括号函数体被正确捕获,这意味着在参数传递期间引起的任何异常(如果发生任何异常)不会被捕获在这个try-catch块中。

The Third Syntax:
It ensures that any exception thrown from betwen the starting brace of the try block inside the function body gets caught appropriately, It would mean any exception caused during the argument passing(if any can occur) will not be caught in this try-catch block.

EDIT:

在构造函数和方法中使用第二种语法(function-try-block)时要考虑的一些指导原则析构函数:


Some guidelines to be considered while using the second syntax(function-try-block) in constructors & destructors:

根据C ++标准,


不抛出(重新抛出原始异常,或抛出一些新的东西),并且控制到达构造函数或析构函数的catch块的结尾,然后原始异常自动重新抛出。

If the catch block does not throw (either rethrow the original exception, or throw something new), and control reaches the end of the catch block of a constructor or destructor, then the original exception is automatically rethrown.

简单来说:

构造函数或析构函数 - try-block的处理程序代码必须通过发出一些异常来完成。

In Simple words:
A constructor or destructor function-try-block's handler code MUST finish by emitting some exception.

指南1:

构造函数 - try-block处理程序只有一个目的 - 翻译异常。

从析构函数中抛出异常是一个糟糕的主意,请查看 此处 以了解原因。

指南2:

析构函数 - try-blocks没有实际用途。不应该有任何东西让他们检测,即使有东西要检测,因为邪恶的代码,处理程序不是非常有用的做任何事情,因为它不能抑制异常。

Throwing a exception from destructors is an bad idea, Take a look here to know why.
Guideline 2:
Destructor function-try-blocks have no practical use at all. There should never be anything for them to detect, and even if there were something to detect because of evil code, the handler is not very useful for doing anything about it because it can not suppress the exception.

指南3:

总是在构造函数或析构函数体中清除本地try-block处理程序中的非托管资源获取,或析构函数 - try-block处理程序。

Guideline 3:
Always clean up unmanaged resource acquisition in local try-block handlers within the constructor or destructor body, never in constructor or destructor function-try-block handlers.

对于标准风扇:

C ++标准,第15.3节第15段:


的构造函数的功能块,程序是不成形的。

If a return statement appears in a handler of the function-try-block of a constructor, the program is ill-formed.

C ++标准,第15.3节第16段:


如果控制到达构造函数或析构函数的函数try块的处理程序的末尾,则抛出异常。否则,当控制到达函数try-block(6.6.3)的处理程序的结尾时,函数返回。流出一个函数try块的结束相当于没有值的返回;这导致在值返回函数(6.6.3)中的未定义行为。

The exception being handled is rethrown if control reaches the end of a handler of the function-try-block of a constructor or destructor. Otherwise, a function returns when control reaches the end of a handler for the function-try-block (6.6.3). Flowing off the end of a function-try-block is equivalent to a return with no value; this results in undefined behavior in a value-returning function (6.6.3).






参考文献

请查看此必须阅读资源 此处 更多详情&解释。


References:
Have a look at this must read resource here for more details & explanation.

这篇关于函数的try-catch语法之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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