Visual LISP, AutoLISP and General Customization
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Return to Calling Function after Trapping Error in Subroutine
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
I've got a subroutine that gets the centroid of a closed polygon (lwpoly). It appears to fail if the polygon has any zero length line segments. I've added a trap that catches the error and "highlights" the offending polygon, but it stops there and does not return to the main function that called the subroutine. Is there a way to get back to the main function from the trap?
Here's the subroutine (original function by _gile (Autodesk LISP Forum 9-18-2006):
(defun return-centroid (lwpoly space / obj Region Centroid)
(setq *error* trap1)
(setq obj (vlax-ename->vla-object lwpoly))
(setq Region (vlax-invoke space 'addRegion (list obj)))
(setq Centroid (trans (vlax-get (car Region) 'Centroid) 1 0))
(vla-delete (car Region))
Centroid
)
(defun trap1 (errmsg)
(prompt "\nUnable to determine centroid of highlighted polygon")
(setq centroid nil)
(command "pedit" pe "w" 10 "")
(setq *error* temperr)
)
Re: Return to Calling Function after Trapping Error in Subroutine
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
The LISP (*error*) handler oncly cleans things up, it doesn't return to the program flow. What I've done for invalid centroids is use the (vl-catch-all-apply...) and (vl-catch-all-error-p...) functions when getting the Centroid property, allowing me to check if getting the property threw an error or not while still continuing with the program flow.
If you are going to fly by the seat of your pants, expect friction burns.
Adopt. Adapt. Overcome. Or be overcome.

Re: Return to Calling Function after Trapping Error in Subroutine
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Thanks. Looks like I'll have to add the vl-catch-all functions.

