Author Topic: if #<%catch-all-apply-error%>  (Read 1313 times)

0 Members and 1 Guest are viewing this topic.

mailmaverick

  • Bull Frog
  • Posts: 493
if #<%catch-all-apply-error%>
« on: September 01, 2016, 06:57:46 AM »
I have a function which sometimes returns error.

For example (setq val (funcname argument1 argument 2))

Now sometimes val contains correct value. But sometimes, if there is an error within the function, it returns #<%catch-all-apply-error%>

(if (equal val #<%catch-all-apply-error%>) does not work.

How to check whether the function has returned correct value or error ?

Thanks.

kpblc

  • Bull Frog
  • Posts: 396
Re: if #<%catch-all-apply-error%>
« Reply #1 on: September 01, 2016, 07:13:59 AM »
Smth like this
(if (vl-catch-all-error-p (setq  err (vl-catch-all-apply (function (lambda () ... )))))
(princ (strcat "\Error : " (vl-catch-all-error-message err)))
)
Sorry for my English.

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: if #<%catch-all-apply-error%>
« Reply #2 on: September 01, 2016, 04:46:49 PM »
Stick with kpblc's suggestion.
But I like alternatives, even if they seem useless... so:
Code: [Select]

(if (not (= 'VL-CATCH-ALL-APPLY-ERROR (type (setq err (vl-catch-all-apply (function (lambda () ... )))))))
< do stuff >
(princ (strcat "\Error : " (vl-catch-all-error-message err)))
)
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: if #<%catch-all-apply-error%>
« Reply #3 on: September 01, 2016, 05:21:54 PM »
Given a couple quick defuns ...

Code: [Select]
(defun _Try ( try_statement / try_catch try_result )
    (if
        (vl-catch-all-error-p
            (setq try_catch
                (vl-catch-all-apply
                    (function
                        (lambda ( )
                            (setq try_result (eval try_statement))
                        )
                    )
                )
            )
        )
        (setq *try_errors* ;; global
            (cons
                (mapcar 'vl-prin1-to-string
                    (list
                        try_statement
                        (vl-catch-all-error-message try_catch)
                    )
                )   
                *try_errors* ;; global
            )
        )
    )
    try_result
)

Code: [Select]
(defun c:ShowErrors ( )
    (foreach error *try_errors*
        (princ "\n")
        (princ error)
    )
    (princ)
)

Code: [Select]
(defun c:ClearErrors ( )
    (setq *try_errors* nil)   
    (princ)
)

Then ...

(_Try '(/ 1 2.0))    >> 0.5 (normal execution)
(_Try '(/ 1 0))      >> nil (error caught)
(_Try '(strlen nil)) >> nil (error caught)
(_Try '(* "2" 2))    >> nil (error caught)


ShowErrors (LIFO):

((* "2" 2) "bad argument type: numberp: \"2\"")
((STRLEN nil) "bad argument type: stringp nil")
((/ 1 0) "divide by zero")


Cheers.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

mailmaverick

  • Bull Frog
  • Posts: 493
Re: if #<%catch-all-apply-error%>
« Reply #4 on: September 03, 2016, 12:35:06 PM »
Stick with kpblc's suggestion.
But I like alternatives, even if they seem useless... so:
Code: [Select]

(if (not (= 'VL-CATCH-ALL-APPLY-ERROR (type (setq err (vl-catch-all-apply (function (lambda () ... )))))))
< do stuff >
(princ (strcat "\Error : " (vl-catch-all-error-message err)))
)

Grrr1337's code did exactly what i wanted. Thanks to him and all others.


Grrr1337

  • Swamp Rat
  • Posts: 812
Re: if #<%catch-all-apply-error%>
« Reply #5 on: September 03, 2016, 04:21:20 PM »
Notice that
Code: [Select]
(not (= 'VL-CATCH-ALL-APPLY-ERROR (type ...is the same as
Code: [Select]
(not (vl-catch-all-error-p ...
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg