Handling 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 use vl-catch-all-apply to intercept errors returned by ActiveX methods.

To intercept errors returned by ActiveX methods

  1. Load the following function and invoke it by issuing (init-motivate) at the VLISP Console prompt:
    (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.

  2. The GetBoundingBox method obtains two points of a box enclosing a specified object, returning those points in variables you supply to the method. (See Using ActiveX Methods That Return Values in Arguments for an example using this.) The following code obtains a pointer to the last object added to a drawing's model space and uses vla-getboundingbox to obtain the points enclosing the object:
    (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 this code and run it by issuing (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:

    ; error: Automation Error. Invalid extents
    

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

  3. By invoking vla-getboundingbox through the vl-catch-all-apply function, you can intercept errors returned by ActiveX. Load the following code and run it by issuing (bndcatch) at the Console prompt:
    (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.

    Catching 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.