Share
 
 

Example: Intercept an Error Returned by an ActiveX Method (AutoLISP/ActiveX)

The vl-catch-all-apply function allows you to intercept errors returned by ActiveX methods, and decide how the program should continue.

Note: ActiveX support in AutoLISP is limited to Windows only.

Create a new AutoLISP source code file

  • Create a new LSP file and save it with the name interceptActiveXErrors.

Create a new Ray object in model space

  • In the new file, enter
    (defun init-motivate ()
      (vl-load-com)
      (setq mspace
        (vla-get-modelspace
          (vla-get-activedocument (vlax-get-acad-object))
        )
      )
      (vla-addray mspace (vlax-3d-point 0 0 0) (vlax-3d-point 1 1 0))
    )

Get the bounding box for the last object added to model space

  • In the new file, enter
    (defun bnddrop (/ bbox)
      (setq bbox (vla-getboundingbox
        (vla-item mspace (- 1 (vla-get-count mspace)))
          'll
          'ur
        )
      )
      (list "Do something with bounding box." bbox)
    )

Load and run the code

  1. Save the LSP file and then load it into AutoCAD.
  2. At the AutoCAD Command prompt, enter (bnddrop).

    Because a Ray object extends to infinity, it is not possible to enclose it with a box, and GetBoundingBox results in the following error:

    ; error: Automation Error. Invalid extents

    If this code were part of your application program, execution would halt at this point.

Catch the error created by the vla-GetBounding Box function

  • In the new file, enter
    (defun bndcatch (/ bbox)
      (setq bbox (vl-catch-all-apply
        'vla-getboundingbox
          (list (vla-item mspace (- 1 (vla-get-count mspace)))
            'll
            'ur
          )
        )
      )
      (if (vl-catch-all-error-p bbox)
        (list "Exception: " (vl-catch-all-error-message bbox))
        (list "Do something with bounding box." bbox)
      )
    )

    This function uses vl-catch-all-apply to call vla-getboundingbox. It passes vl-catch-all-apply two arguments: the symbol naming the function being called ('vla-getboundingbox) and a list of arguments to be passed to vla-getboundingbox. If the GetBoundingBox method completes successfully, vl-catch-all-apply stores the return value in variable bbox. If the call is unsuccessful, vl-catch-all-apply stores an error object in bbox.

    At this point in the bnddrop function, vla-getboundingbox was issued directly, an error resulted, and execution halted. But in bndcatch, vl-catch-all-apply intercepts the error and program execution continues.

    A call to vl-catch-all-error-p checks the return value from vl-catch-all-apply and returns T if it is an error object, nil otherwise. If the return value is an error object, as it would be in this example, the function issues vl-catch-all-error-message to obtain the message from the error object. Program execution continues from this point.

Was this information helpful?