vl-catch-all-apply to intercept errors returned by ActiveX methods.
When ActiveX methods fail, they raise exceptions rather than returning error values that your program can interpret. If your program uses ActiveX methods, you must prepare it to catch exceptions, otherwise the program halts, leaving the user at a Command prompt. You can useTo intercept errors returned by ActiveX methods
(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))
)
This function adds a ray object to the current model space. A ray has a finite starting point and extends to infinity.
(defun bnddrop (/ bbox)
(setq bbox (vla-getboundingbox
(vla-item mspace (- 1 (vla-get-count mspace)))
'll
'ur
)
)
(list "Do something with bounding box." bbox)
)
(bnddrop) at the Console prompt. Because a ray extends to infinity, it is not possible to enclose it with a box, and GetBoundingBox results in the following error:
Load this code and run it by issuing; error: Automation Error. Invalid extents
If this code were part of your application program, execution would halt at this point.
(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)
)
)
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.
This function usesbnddrop 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.
At this point in thevl-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.
A call toCatching Errors and Continuing Program Execution includes a non-ActiveX example that uses the vl-catch-* functions to intercept errors. For additional information on these functions, see the AutoLISP Reference.