Dealing with Errors in AutoCAD Programs29 Oct, 2015 By: Andrew G. Roe
Errors are bound to happen, but it’s simple to incorporate error handling in your VB.NET and VBA code.
Regardless of your programming skill level, errors can occur whenever someone runs your program. The key is handling those errors correctly and efficiently. In most of my previous articles on AutoCAD programming, error handling has been omitted for brevity and clarity in demonstrating other programming concepts. In this article, we’ll look more closely at error handling in two different environments: Visual Basic for Applications (VBA) and Visual Basic.NET (VB.NET).
While VBA has declined in popularity among AutoCAD programmers in the past decade, Autodesk still unofficially supports it with a downloadable VBA enabler, so AutoCAD users can continue using legacy VBA routines (see “VBA Lives On with AutoCAD 2016”). VBA’s simplicity provides a good starting point for understanding basic error-handling concepts.
Consider the case where your program needs to perform some basic calculations, such as dividing one number into another to determine a percentage. Errors could occur if the user enters something other than a number (such as a letter), or if the user tries to divide a number by zero. VBA provides several tools to handle these errors, including the On Error statement. To see how this works, try the following steps.
1. If you haven’t already done so, download and install the 32-bit or 64-bit VBA enabler (whichever is applicable to your operating system). The enabler works in AutoCAD and its vertical products.
2. Start AutoCAD.
3. Select the Manage tab on the AutoCAD ribbon, and click Visual Basic Editor. This opens the VBA Interactive Development Environment (IDE), as shown below.
4. From the Insert menu in the VBA IDE, click Module to insert a blank Module.
5. Add the following code into the VBA code window:
Private Sub ErrorHandlingExample() On Error GoTo ErrorHandler Dim Var1 As Double Dim Var2 As Double Dim Pct As Double Var1 = InputBox("Enter a numerator:") Var2 = InputBox("Enter a denominator:") Pct = Var1 / Var2 MsgBox ("Percentage = " & Pct) Exit Sub ErrorHandler: Select Case Err.Number Case 11 MsgBox "You tried to divide by zero." Case 13 MsgBox "You did not enter a number." Case Else MsgBox Err.Description End Select End Sub
6. Click the Play button to run the routine you just created.
7. Enter a number when prompted for the numerator, and enter 0 when prompted for the denominator. You should see an error message in a message box.
8. Click OK to close the information window and return to the VBA IDE.
9. Click the Play button to run the routine again.
10. Enter a letter when prompted for the numerator. You should see a different error message.
If you enter two numbers when prompted, the program runs as intended and provides an answer.
About the Author: Andrew G. Roe
In her easy-to-follow, friendly style, long-time Cadalyst contributing editor and Autodesk Technical Evangelist Lynn Allen guides you through a new feature or time-saving trick in every episode of her popular AutoCAD video tips. Subscribe to the free Cadalyst Video Picks newsletter and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!