Dealing with Errors in AutoCAD Programs

29 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 

Add comment

Note: Comments are moderated and will appear live after approval by the site moderator.


Re: Dealing with Errors in AutoCAD Programs
by: rexxitall
October 31, 2015 - 9: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
AutoCAD Tips!

Lynn Allen

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!

Follow Lynn on TwitterFollow Lynn on Twitter

Which device do you typically use to read content?
A desktop computer / tower workstation
A tablet
A smartphone
A laptop or mobile workstation
I regularly use both a desktop computer and a smartphone for this purpose
I regularly use another combination of devices for this purpose
I prefer to print out articles from the website and read them on paper
Submit Vote

Download Cadalyst Magazine Special Edition