[Mercury Feature request] Structured On Error Resume Next

For VB6 backward compatibility, VB has the On Error Resume Next construct.

An example of this:

 'Some code
 On Error Resume Next 'Ignore the errors on the next code
 'Some code  where the errors are ignored
 'Can be multiple lines
 On Error Goto 0
 'Rest of the code

To write this with Structured Error Handling, we need a try - catch block for each statement:

'Some code
Try 'Ignore the errors on the next code
    'Some code  where the errors are ignored
Catch
End Try
Try
    'Can be multiple lines - but need multiple Try statements
Catch
End Try
'Rest of the code

This is ugly code and a lot of typing work.
So I propose a structured way to do the On Error Resume Next:

 'Some code
 Try Resume Next 'Ignore the errors on the next code
     'Some code  where the errors are ignored
     'Can be multiple lines
 End Try
 'Rest of the code

A more advanced version in the VB6 syntax:

On Error Resume Next
'Set Open file
If Err.Number <> 0 then
    MsgBox("Could not open the file!")
Else
    'Write to the file
    If Err.Number <> 0 then
        MsgBox("Could not write to the file")
    Else
        'Move file to other location
        If Err.Number <> 0 then
            MsgBox("Could not move the file")
        Else
            MsgBox("File has been opened, written and moved")
        End If
    End If
End If

This would become - with the structured Resume Next:

Try Resume Next With ex As Exception 'can not be any other type than Exception!
     'Set Open file
     If ex IsNot Nothing then
         MsgBox("Could not open the file!")
     Else
         'Write to the file
         If ex IsNot Nothing then
             MsgBox("Could not write to the file")
         Else
             'Move file to other location
             If ex IsNot Nothing then
                 MsgBox("Could not move the file")
             Else
                 MsgBox("File has been opened, written and moved")
             End If
         End If
     End If
  End Try

A simple way to bring On Error Resume Next in the 21st century.

One point of interest: It is impossible to throw an exception as it will be ignored (resume next), but if you do this, it will set the ex variable.

SEH as it is a part of OS gives you full control on what to do with error. You can even restart instruction that caused the error. So read about SEH in MSDN first.

Welcome @btframework.

Can you give me an example of that?
And as you say - part of the OS - how will that work on other OSses?

But, I went to read about it and found:

Examples: https://docs.microsoft.com/en-us/windows/win32/debug/structured-exception-handling
I think the same way: through CPU exception handlers including some compiler support. CPU exception handlers based on interrupts (refer to Intel/AMD documentation. ARM/AVR/PIC more or less identical).
Edited: I also recommend to take a look on “Windows Internals” book that explains SEH very very well.

Remember that this is VB - not C++
VB is about making complex problems easy to solve.

And SEH is a complex problem, made easy by adding a keyword.

I read Windows Internals.
But Again. This is VB. In this case: Mercury. That means Cross Platform.
And Windows Internals will not work on cross platform.

To be usable as Cross Platform solution, the complete OS has to be abstracted away.
The constructs will be usable on any platform - not only windows. And the compiler will compile it to the specifics of the currently used platform. Not the language.

Crossplatform is the myth. If you want to do something good - make it for single platform only. Of course if your application is just a calculator it can be crossplatform. But if you need something complex (hardware/SEH) - make it for single platform only. No exceptions.

That goes for apps, yes. Not for programming languages.

1 Like