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.