简体   繁体   English

触发事件时在函数调用中引发异常

[英]Raise exception in function call when event is triggered

I have a VB.NET class Flasher used to flash a device with some firmware via the .Flash() method. 我有一个VB.NET类Flasher用于通过.Flash()方法刷新具有某些固件的设备。 However, during the flashing process an error might occur, in which case an event will be triggered. 但是,在刷新过程中可能会发生错误,在这种情况下将触发事件。 I cannot figure out how to handle this event and raise an exception in the callstack of .Flash() . 我无法弄清楚如何处理此事件并在.Flash()的调用堆栈中引发异常。

Class Flasher
    Private myDevice As New FlashLib.Device

    Public Function Flash() As String
        AddHandler myDevice.ErrorEvent, AddressOf OnError
        '... Do flashing stuff here that might send our `ErrorEvent` ...'
        myDevice.flash_firmware("myfirmware.fw")
        myDevice.restart()
        Threading.Thread.Sleep(5000)
        return "OK"
    End Function

    Public Sub OnError(ByRef Err As ErrEventArgs)
        errMsg = String.Format(
            "Device threw an error during flashing process. Type: {0}",
            Err.error)
        Throw New System.Exception(errMsg)
    End Sub

The Flasher class depends on a C# library FlashLib that is the originator of the ErrorEvent : Flasher类取决于C#库FlashLib ,它是ErrorEvent的始发者:

namespace FlashLib
{
    public delegate void ErrorEventHandler(ErrEventArgs err);

    public interface Device
    {
        event ErrorEventHandler ErrorEvent;
    }
}

The problem is when I instantiate Flasher and call .Flash() the idea is if there's an error event, the caller of .Flash() will be thrown an exception, however what happens is in the middle of .Flash() execution jumps out of that function and into OnError() . 问题是当我实例化Flasher并调用.Flash() ,想法是如果发生错误事件, .Flash()的调用者将引发异常,但是在.Flash()执行的中间跳出的是该函数并进入OnError() Throwing an exception from here isn't caught by my catch clause: 我的catch子句未捕获从此处引发异常的情况:

Dim flasher As New Flasher
Try
    flasher.Flash()
Catch
    Debug.Print("There was a problem with flashing")
End Try

Instead the exception rises to the top and I get shown an error message box. 相反,异常上升到顶部,并且显示错误消息框。 Then execution continues from where it left off in the Flash() method and carries on. 然后,从Flash()方法中中断的地方继续执行并继续执行。 It seems that events in VB.NET behave like software interrupts: jump out of whatever you're doing into the handler, and then resume where you were before? 似乎VB.NET中的事件的行为就像软件中断一样:跳出您在处理程序中所做的任何操作,然后恢复之前的状态? I need the caller of Flash() to be able to catch an exception when an ErrorEvent is triggered. 我需要Flash()的调用方能够在触发ErrorEvent时捕获异常。

Rather than your OnError method throwing an exception, have it set a flag that you can test for in Flash and then then throw the exception if you find it set. 不是让您的OnError方法抛出异常,而是让它设置一个可以在Flash测试的标志,然后在发现异常的情况下抛出该异常。 Eg 例如

Class Flasher
    Private myDevice As New FlashLib.Device
    Private errMsg As String

    Public Function Flash() As String
        AddHandler myDevice.ErrorEvent, AddressOf OnError
        '... Do flashing stuff here that might send our `ErrorEvent` ...'

        If errMsg IsNot Nothing Then
            Throw New System.Exception(errMsg)
        End If

        myDevice.flash_firmware("myfirmware.fw")
        myDevice.restart()
        Threading.Thread.Sleep(5000)
        return "OK"
    End Function

    Public Sub OnError(ByRef Err As ErrEventArgs)
        errMsg = String.Format(
            "Device threw an error during flashing process. Type: {0}",
            Err.error)
    End Sub

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

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