Learn the basics of AutoCAD and More!

Lesson 5.17 – Basic Error Handling in AutoLISP

🚨 Lesson 5-17 – Basic Error Handling in AutoLISP

Learn how to use the *error* function to prevent crashes and restore your drawing environment after unexpected issues.


πŸ“š What You’ll Learn

By the end of this lesson, you’ll be able to:

  • Implement custom error handlers using the *error* function
  • Restore environment settings like OSNAP or layer states
  • Safely exit routines when something goes wrong
  • Write more professional, resilient AutoLISP tools

🧠 Why It Matters

Even a small bug can disrupt your workflow or leave settings changed in your drawing session. With proper error handling, you can trap problems, display helpful messages, and return AutoCAD to a clean state automatically.


πŸ› οΈ Tools You’ll Use

Tool / FunctionPurpose
*error*Custom global error function
(command ...)Reset settings during cleanup
setq / setvarStore and restore AutoCAD system variables
(princ)Suppress command line clutter

🧭 Lesson Structure

1️⃣ The Anatomy of an AutoLISP Error

When something goes wrongβ€”such as a canceled command or an invalid selectionβ€”AutoCAD may:

  • Display a system error message
  • Exit your function prematurely
  • Leave system variables (like OSNAP) in a changed state

This is why cleanup is important.


2️⃣ Introducing the *error* Function

Here’s a basic example:

(defun my-error (msg)
(prompt (strcat "\\n⚠ Error: " msg))
(setvar "OSMODE" 0) ; Restore OSNAP to default
(setq *error* nil) ; Reset handler
(princ)
)

Set this function as the error handler at the start of your routine:

(setq olderror *error*
*error* my-error)

3️⃣ Complete Pattern with Error Handling and Cleanup

(defun c:safeoffset ( / *error* oldsnap ent dist )

;; Save OSNAP setting
(setq oldsnap (getvar "OSMODE"))

;; Define error handler
(defun my-error (msg)
(prompt (strcat "\\n⚠ Error: " msg))
(setvar "OSMODE" oldsnap)
(setq *error* olderror)
(princ)
)

;; Set the error handler
(setq olderror *error* *error* my-error)

;; Routine logic
(setq ent (car (entsel "\\nSelect object to offset: ")))
(setq dist (getdist "\\nEnter offset distance: "))
(command "_.offset" dist ent pause "")

;; Restore environment
(setvar "OSMODE" oldsnap)
(setq *error* olderror)
(princ)
)

4️⃣ Error Types You Can Catch

Example Error MessageWhat It Means
Function cancelledUser hit ESC or clicked cancel
bad argument typeFunction expected a number but got nil
no function definitionMisspelled function or uninitialized call

Use (alert msg) instead of (prompt) if you want to show a dialog box.


βœ… Lesson Checklist

TaskCompleted
Created a routine with a custom *error* handler☐
Saved and restored at least one system variable☐
Tested user cancellation or bad input☐
Used (princ) to suppress command output☐

πŸ“Œ Quick Tips

TipWhy It Helps
Always restore system variablesPrevents unexpected drawing behavior
Reset *error* to its previous valueAvoids recursive or unintended trapping
Use princ oftenMakes command output clean and quiet
Name your error handler clearlyEasier to reuse and debug

🧩 Real-World Applications

ScenarioWhy Error Handling Helps
User cancels commandRestores previous settings and shows friendly output
Invalid selectionPrevents crash and prompts user to retry
OSNAP or layer settingsMaintains consistent drawing environment

πŸ“ Files and Resources

File / ResourceDescription
safeoffset_example.lspComplete routine with full error handling
error_template.lspBlank template to copy and modify
error_handling_checklist.docxPrintable checklist to guide proper cleanup

πŸ“– Review Table

FunctionPurposeExample
*error*Sets global error function(setq *error* my-error)
setvarRestores AutoCAD system settings(setvar "OSMODE" oldsnap)
entsel, getdistGets user input(getdist "\\nDistance: ")
commandCalls AutoCAD commands from LISP(command "OFFSET" dist ent pause "")