类析构函数中struct字段值检索的可靠性 [英] Reliability of struct field value retrieval in class destructor

查看:58
本文介绍了类析构函数中struct字段值检索的可靠性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实施以下课程时:

When implementing the following class:

public class MyClass
{
   private readonly int myInt = 4;

   ~MyClass()
   {
      if (myInt != 4)
         throw new Exception();
   }
}



是否有可能发生异常抛出的情况?显然,我担心在实例破坏期间过早处理现场价值。如果这是可靠的话,对我来说是有意义的,但我很想对此有第二个意见。


Is it possible for a condition to occur in which the exception will be thrown? Obviously I''m concerned with premature disposal of field values during instance destruction. It''d make sense to me if this were reliable but I''d love to have a second opinion on this.

推荐答案

在析构函数执行期间发生的异常是特别值得一提。如果在析构函数执行期间发生异常,并且未捕获该异常,则终止该析构函数的执行并调用基类的析构函数(如果有)。如果没有基类(如对象类型的情况)或者没有基类析构函数,则抛弃异常。



参见这个 [ ^ ] MSDN文章





干杯,

Edo
Exceptions that occur during destructor execution are worth special mention. If an exception occurs during destructor execution, and that exception is not caught, then the execution of that destructor is terminated and the destructor of the base class (if any) is called. If there is no base class (as in the case of the object type) or if there is no base class destructor, then the exception is discarded.

See this [^] MSDN article


Cheers,
Edo


这篇关于类析构函数中struct字段值检索的可靠性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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