DBedrenko
DBedrenko

Reputation: 5039

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. 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().

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:

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(). Throwing an exception from here isn't caught by my catch clause:

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. 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? I need the caller of Flash() to be able to catch an exception when an ErrorEvent is triggered.

Upvotes: 0

Views: 97

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54457

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. E.g.

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

Upvotes: 1

Related Questions