π¨ 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 / Function | Purpose |
---|---|
*error* | Custom global error function |
(command ...) | Reset settings during cleanup |
setq / setvar | Store 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 Message | What It Means |
---|---|
Function cancelled | User hit ESC or clicked cancel |
bad argument type | Function expected a number but got nil |
no function definition | Misspelled function or uninitialized call |
Use
(alert msg)
instead of(prompt)
if you want to show a dialog box.
β Lesson Checklist
Task | Completed |
---|---|
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
Tip | Why It Helps |
---|---|
Always restore system variables | Prevents unexpected drawing behavior |
Reset *error* to its previous value | Avoids recursive or unintended trapping |
Use princ often | Makes command output clean and quiet |
Name your error handler clearly | Easier to reuse and debug |
π§© Real-World Applications
Scenario | Why Error Handling Helps |
---|---|
User cancels command | Restores previous settings and shows friendly output |
Invalid selection | Prevents crash and prompts user to retry |
OSNAP or layer settings | Maintains consistent drawing environment |
π Files and Resources
File / Resource | Description |
---|---|
safeoffset_example.lsp | Complete routine with full error handling |
error_template.lsp | Blank template to copy and modify |
error_handling_checklist.docx | Printable checklist to guide proper cleanup |
π Review Table
Function | Purpose | Example |
---|---|---|
*error* | Sets global error function | (setq *error* my-error) |
setvar | Restores AutoCAD system settings | (setvar "OSMODE" oldsnap) |
entsel , getdist | Gets user input | (getdist "\\nDistance: ") |
command | Calls AutoCAD commands from LISP | (command "OFFSET" dist ent pause "") |