繁体   English   中英

class 库中的 C# 异常 - 事件处理程序

[英]C# Exception in class library - event handler

我正在开发一个 class 库,它使用Form来捕获特定的WndProc消息并在之后处理它们。 处理包括文件操作。 然后库将处理后的值发布为公共属性或事件。

问题:当处理引发异常时我该怎么办?
如果我让它冒泡给调用者,它需要在哪里捕获? 因为它不是需要调用的函数/方法。

库 Class:

 public event EventHandler<CmmStateChangedEventArgs> CmmStateChanged;
 public event EventHandler<MeasurementInfoEventArgs> MeasurementInfoChanged;
 public event EventHandler<MeasurementPlanInfoEventArgs> MeasurementPlanInfoChanged;

 public MeasurementPlanInfo MeasurementPlanInfo { get; private set; }
 public MeasurementInfo MeasurementInfo { get; private set; }
 public Status CMMStatus { get; private set; }

 // event handler for internal form WndProc event
 // _messageForm is a empty invisible form which catches specific WndProc messages and invokes a event if necessary
 private async void _messageForm_CMMStateChanged(object sender, CmmStateChangedEventArgs e)
    {
        // this could throw
        var command = await _stateManager.GetCommandFileAsync(Configuration.CMMObserverFolderPath);
        var observer = await _stateManager.GetObserverFileAsync(Configuration.CMMObserverFolderPath);

        var measInfo = new MeasurementInfo();

        switch (e.Status)
        {
            case Status.Running:
                if (command.state == "set_cnc_start")
                {
                    // this could throw if observer is null
                    measInfo = new MeasurementInfo()
                    {
                        MeasurementPlanId = observer.planid,
                        ControllerType = observer.controllertyp,
                        DeviceGroup = observer.devicegroup,
                        FirmwareRevision = observer.firmWareRevision,
                        OperatorId = observer.operid,
                        PartNumber = observer.partnbinc,
                        MeasurementPlanFileName = command.planPath,
                    };

                    MeasurementInfo = measInfo;
                    MeasurementInfoChanged?.Invoke(this, new MeasurementInfoEventArgs() { MeasurementInfo = measInfo });

                }
                break;

            case Status.Finished:
                measInfo = new MeasurementInfo()
                {
                    MeasurementPlanId = observer.planid,
                    ControllerType = observer.controllertyp,
                    DeviceGroup = observer.devicegroup,
                    FirmwareRevision = observer.firmWareRevision,
                    OperatorId = observer.operid,
                    PartNumber = observer.partnbinc,
                    ChrFilePath = command.chrPath,
                    HdrFilePath = command.hdrPath,
                    FetFilePath = command.fetPath
                };

                MeasurementInfo = measInfo;
                MeasurementInfoChanged?.Invoke(this, new MeasurementInfoEventArgs() { MeasurementInfo = measInfo });
                break;

            case Status.Paused:
            case Status.Stopped:
            case Status.Exception:
            default:
                break;
        }

        CMMStatus = e.Status;
        CmmStateChanged?.Invoke(this, new CmmStateChangedEventArgs() { Status = e.Status });
    }

调用 Class:

    public MainForm()
    {
        InitializeComponent();

        apiConfiguration = new ApiConfiguration();

        // where catch possible exception ?
        api = new Calypso()
            .Configure(apiConfiguration)
            .Initialize();

        api.CmmStateChanged += Api_CmmStateChanged;
        api.MeasurementInfoChanged += Api_MeasurementInfoChanged;
        api.MeasurementPlanInfoChanged += Api_MeasurementPlanInfoChanged;
    }

这取决于。 哪个 scope 可以解释异常并采取措施(API 或调用方)? 什么被抛出? 抛出异常是否意味着调用者的 state 现在不同步并且事情即将变得糟糕? API 可以尝试解决这样的问题吗? 下一个工作变更事件是否会让调用者完全恢复? 调用者是否可能对这些“周期性故障(?)”感兴趣 - 可能会添加消息丢失事件。

如果异常是严重失败,问题是在调用者的哪里处理这个问题:我建议捕获异常并添加失败事件,然后调用者可以做出最好的响应。 或者,初始化可以接受要调用的回调操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM