Codegen for On Error Resume Next

VB has a "On Error Resume Next", which tells each line to swallow exceptions and just keep executing to the next line. It's kind of like a try-catch around every single line.

It may be tempting for C++ developers to make fun of VB for this, but this is a lot like programming with HRESULT (or any error handling strategy based on return codes) when you forget to check the return value. And as we look at the codegen below, VB's code could turn out to be even more efficient than the unmanaged error handling equivalent.


So in this VB snippet, the exception is is swallowed and "Next" is printed.

 Module Module1

    Sub Main()
        On Error Resume Next
        Throw New Exception("Bong!")

    End Sub

End Module


If you're wondering what the codegen is like, you can always compile and then view the IL in ildasm. Or you can view it in Reflector as C#  instead of IL, which is easier to comprehend.


public static void Main()
    // This item is obfuscated and can not be translated.
    int VB$ResumeTarget;
        int VB$CurrentStatement;
        int VB$ActiveHandler = -2;
        VB$CurrentStatement = 2;
        VB$CurrentStatement = 3;
        throw new Exception("Bong!");
        VB$CurrentStatement = 4;
        goto Label_009E;
        VB$ResumeTarget = 0;
        switch ((VB$ResumeTarget + 1))
            case 1:
                goto Label_0001;

            case 2:
                goto Label_0009;

            case 3:
                goto Label_0016;

            case 4:
                goto Label_0023;

            case 5:
                goto Label_009E;

                goto Label_0093;
        VB$ResumeTarget = VB$CurrentStatement;
        switch (((VB$ActiveHandler > -2) ? VB$ActiveHandler : 1))
            case 0:
                goto Label_0093;

            case 1:
                goto Label_0035;
    catch (object obj1) when (?)
        ProjectData.SetProjectError((Exception) obj1);
        goto Label_0059;
    throw ProjectData.CreateProjectError(-2146828237);
    if (VB$ResumeTarget != 0)



So you can see it's just putting the entire region in a try/catch block, and then using a switch table to jump back to the appropriate line.  It has a "Current Statement" variable to track the last successful line to execute before an exception may have been thrown, and then switches to the next line on the exception path.

The switch table may seem evil at first, but remember that in native code, all those IfFailGoto() checks to propagate return results also add up to a lot of switching code. In this case, the branches are at least optimized into a single switch table as opposed to scattered branch code.