Dealing with Errors in AutoCAD Programs

28 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
      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.

1 2 

About the Author: Andrew G. Roe

Andrew G. Roe

Add comment


Re: Dealing with Errors in AutoCAD Programs
by: rexxitall
October 31, 2015 - 10:16am
Ther is something more to say. Not all errors has to be "handled" - especially not if you are in coding process. A error handler or prevent error handler is also often a case for GOTO. A few right placed gotos saves endless if then structures. Some sample code below. Small dirty but efficiant :) And for sure just as a sample. function dosomething as boolean dosomething =false txt=inputbox("test") if txt="" goto raus 'prevent error exit on error resume next 'we ignore errors a while err.clear 'clean error state k=val(txt) l=1/k .. ... .... 'so if somewhere a error ocurs we are out ' and function will be false :) if err.number <>0 then goto raus err.clear on error goto 1 'switch error on again use 'standard error handler of VBA ... ... on error goto errorhandler .... 'tail of function no further real processing dosomething=true will only be true if everythig went right errhandler: 'anoy and frustrate the user ;) call errorhandler("dosomething", err) raus: 'german word for exit ' i use this often just to exit a function on user abort yu may also use err.raise to create a fake error just for this purpose end sub Sub errorhandler(byval err,function_name as string ) put the rrorhandler somewhere as a function msgbox function_name & " " & err.description end sub
More News and Resources from Cadalyst Partners

For Mold Designers! Cadalyst has an area of our site focused on technologies and resources specific to the mold design professional. Sponsored by Siemens NX.  Visit the Equipped Mold Designer here!

For Architects! Cadalyst has an area of our site focused on technologies and resources specific to the building design professional. Sponsored by HP.  Visit the Equipped Architect here!