class 库中的 C# 异常 - 事件处理程序
C# Exception in class library - event handler
我正在开发一个 class 库,它使用 Form
来捕获特定的 WndProc
消息并在之后处理它们。处理包括文件操作。然后库将处理后的值发布为 public 属性或事件。
问题:然后处理抛出异常怎么办?
如果我让它冒泡给调用者,它需要在哪里被捕获?因为它不是需要调用的 function/method。
图书馆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;
}
视情况而定。哪个范围可以解释异常并采取行动(API 或调用者)?什么被抛出?抛出异常是否意味着调用者的状态现在不同步并且事情即将变坏? API 可以尝试解决这样的问题吗?下一个工作变更事件是否会让调用者完全恢复?来电者是否可能对这些“周期性故障(?)”感兴趣 - 也许添加消息丢失事件。
如果异常是严重失败,并且问题是在调用方的何处处理此异常:我建议捕获异常并添加失败事件,然后调用方可以尽其所能做出响应。或者,初始化可以接受要调用的回调操作。
我正在开发一个 class 库,它使用 Form
来捕获特定的 WndProc
消息并在之后处理它们。处理包括文件操作。然后库将处理后的值发布为 public 属性或事件。
问题:然后处理抛出异常怎么办?
如果我让它冒泡给调用者,它需要在哪里被捕获?因为它不是需要调用的 function/method。
图书馆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;
}
视情况而定。哪个范围可以解释异常并采取行动(API 或调用者)?什么被抛出?抛出异常是否意味着调用者的状态现在不同步并且事情即将变坏? API 可以尝试解决这样的问题吗?下一个工作变更事件是否会让调用者完全恢复?来电者是否可能对这些“周期性故障(?)”感兴趣 - 也许添加消息丢失事件。
如果异常是严重失败,并且问题是在调用方的何处处理此异常:我建议捕获异常并添加失败事件,然后调用方可以尽其所能做出响应。或者,初始化可以接受要调用的回调操作。