在构造函数结束时抛出异常是否会损坏对象? [英] Does throwing exception at the end of constructor damage the object?

查看:191
本文介绍了在构造函数结束时抛出异常是否会损坏对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用与外部设备通信的软件。器件需要一组初始化值(calibrationData)。这些设备的校准数据不同。在第一版本中,校准数据可以由用户选择,因此用户可以通过事故加载在不同部件上获得的校准数据。设备将工作,但将测量不正确。
我有

I'm working on a software communicating with external device. The device requires a set of initialization values (calibrationData). Those calibration data differ from piece to piece of this equipment. In first versions the calibrationData can be selected by user and thus the user may by accident load calibrationData obtained on different piece. The device would work, but will measure incorrectly. I have

    public Instrument(CalibrationData calibration)
    {
        _camera = new Camera();
        _driver = new Driver();

        if (_camera.GetUniqueId() != calibration.GetCameraUniqueId())
           throw new WrongCalibrationException("Calibration file was obtained on different equipment.");
         //Don't write anything here. Exception has to be the last code in the constructor.
    }

然后在其他地方

try
{
    instrument = new Instrument(calibration);
}
catch (WrongCalibrationException e)
{
    MessageBox.Show("You tried to load calibration obtained on different device.");
}

我无法在连接到设备。

这个问题实际上包括两个。

This question comprises out of two in fact.


  1. 我的解决方案是否正确?我想自动测试正确校准的使用,而不依赖程序员使用我的代码调用另一个方法(像Instrument.AreYouProperlyCalibrated())

  1. Is my solution correct? I want to test usage of proper calibration automatically and not rely on the programmer using use my code to call another method (Something like Instrument.AreYouProperlyCalibrated())

当异常抛出在构造函数的结尾时,对象是否正确构造?我有点担心,C#在构建器完成后做一些mumbo jumbo,并且这可能在ctor抛出异常时是不同的。

Is the object constructed properly when the exception is thrown at the end of constructor? I'm a bit afraid that C# is doing some mumbo jumbo after the construcor finishes and that this might be different in case the ctor threw an exception.

感谢

推荐答案

实例在构造函数开始之前已经完全存在(确实,你甚至可以完全绕过所有的构造函数,并仍然获得一个有效的实例) - 它只是意味着没有执行的任何初始化代码将不会执行。

The instance already fully exists before the constructor begins (indeed, you can even completely bypass all constructors and still get a valid instance) - it just means that any initialization code that didn't execute won't have executed.

例如,虽然不是一个好主意,但您可以在构造函数中将类型的对象实例传递出去,即

For example, while it isn't a good idea, you can pass the object instance out of the type during the constructor, i.e.

_camera.HereIsMe(this);

SomeExternalObject.Track(this);

所以什么也不会发生可怕会发生,因为到运行时关注这个对象存在像正常,并且必须正确处理。但是,在某些情况下使用工厂更加干净:

so nothing too terrible will happen, since as far as the runtime is concerned this object exists like normal, and must be handled properly. However, in some cases it is cleaner to use a factory:

public static YourType Create(args) {
    // TODO: perform enough work to validate
    return new YourType(validated args);
}

但重申;如果有问题,那么从构造函数中抛出是不可预料的,并且是无害的。

But to reiterate; if there is a problem, then throwing from the constructor is not unexpected and is not harmful.

这篇关于在构造函数结束时抛出异常是否会损坏对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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