|

AutoLISP Error Handling for Plotting: Restore State and Fail Safely

Use local error handlers, saved system-variable values and per-item error capture so plotting automation does not leave AutoCAD in an unexpected state.

Main idea: A plotting routine is not finished when the happy path works. It is finished when cancellation and failure also leave AutoCAD usable.

Best use: Batch plotting, Page Setup changes and any command that temporarily changes BACKGROUNDPLOT or active Layout.

Start with the manual workflow

List every AutoCAD state value the routine changes. If the user presses Esc halfway through, decide which values must be restored.

Build the automation in small layers

Step 1 — Save original values

Where: Start of command

Do this: Store BACKGROUNDPLOT, active Layout and other temporary settings.

Check: The original state is available.

Step 2 — Use a local *error* handler

Where: Inside the command function

Do this: Restore saved values and report unexpected errors.

Check: Esc or a runtime error does not strand the session.

Step 3 — Catch per-item errors

Where: Batch loop

Do this: Use vl-catch-all-apply around operations that may fail on one Layout.

Check: The batch can log the exact failure.

Step 4 — Avoid destructive recovery

Where: Existing PDFs/DWGs

Do this: Do not delete or overwrite files just to make the routine continue.

Check: Recovery is predictable.

Step 5 — Summarize the result

Where: End of command

Do this: Report created/skipped/failed counts or modified Layouts.

Check: The user knows what happened.

The routine should leave AutoCAD as it found it

Temporary settings are normal in automation. For example, an ActiveX plotting routine may need BACKGROUNDPLOT = 0. The problem is not changing it; the problem is failing to restore the original value when the user presses Esc or a plot throws an error.

The same principle applies to active Layout, temporary layers, FILEDIA and other session state. Store the original value before the change, restore it from both the normal exit path and the local error handler, and keep destructive cleanup out of the recovery code.

What the code should control

  • Local *error*
  • vl-catch-all-apply
  • BACKGROUNDPLOT restore
  • ActiveLayout restore
  • Existing-file policy

What you should deliberately leave manual at first

Do not catch every error and return ‘Done’. Hiding the error is worse than stopping with a clear message.

Failure modes to design for

  • The error handler itself references variables that were never initialized.
  • A nested function replaces the user’s global *error* permanently.
  • The batch continues after a critical standards failure without marking the output.
  • The routine saves partial modifications automatically.

When this becomes a production tool

Add a log file once command-line messages are no longer enough. The log should identify drawing, Layout, action, outcome and error text.

Official Autodesk references

Frequently asked questions

Why restore BACKGROUNDPLOT?

A utility should not permanently change the user’s plotting behavior just because it needed foreground processing temporarily.

Should I continue after every error?

No. Some errors are per-Layout and can be logged; others invalidate the whole batch and should stop it.

What should happen when the user presses Esc?

Restore important state, print a short cancellation message and exit without destructive cleanup.

Explore more in AutoLISP Plotting & PDF Automation.