Implement VB.Net Style Error Trapping

Published on March 9, 2011

As you may know, Microsoft is phasing out VBA and encouraging developers to start using VB.Net. Some VBA code can be imported in to VB.Net and run without any modifications, other code will require extensive modification. The On Error GoTo method of error trapping used in VBA is not supported in VB.Net. In VB.Net this has been replaced by Try-Catch-Finally blocks and looks something like this:

Try
'Processing code goes here
Catch ex as Exception
'Errors raised in the try block transfers control
'to the Catch block
Finally
'This code is always executed
End Try

You can implement this same type of style in your VBA code modules and ease the eventual transition to VB.Net by using this structure:

TRY1:
On Error GoTo CATCH1
'Processing code goes here
GoTo FINALLY1
CATCH1:
'Errors raised in the try block transfers control to the Catch block
FINALLY1:
'This code is always executed
'End Try1

TRY2:
On Error GoTo CATCH2
'Processing code goes here
GoTo FINALLY2
CATCH2:
'Errors raised in the try block transfers control to the Catch block
FINALLY2:
'This code is always executed
'End Try2


Tags:
Like this article? Share it!

One Response to “Implement VB.Net Style Error Trapping”

  1. Envision Newsletter March 2011 - Envision CAD | MicroStation Training & InRoads Training Says:

    April 11th, 2011 at 5:04 pm

    […] VBA: Implement VB.Net Style Error Trapping […]

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Privacy Policy