IF statement error, how lisp can go through the IF contain error and keep run

IF statement error, how lisp can go through the IF contain error and keep run

xuantientutungXUK6E
Advocate Advocate
1,346 Views
6 Replies
Message 1 of 7

IF statement error, how lisp can go through the IF contain error and keep run

xuantientutungXUK6E
Advocate
Advocate

Hello guys,

I have a lisp and it contain a lot of If. When the If catch error in side, the lisp will break and stop.

how i can make lisp keep running if the If statement have error in side.

Example.

(if ( = 1 a)

          then do

                         ( get b) ; here get b is a text or an object like line, 

                          ( + a b) ; command will error

)

; after the IF above is about 4 another IF statement. They will not run because the IF above is error.

 

 

 

 

0 Likes
1,347 Views
6 Replies
Replies (6)
Message 2 of 7

doaiena
Collaborator
Collaborator

Eating up errors is probably the worst way you could write code. The outcomes of your functions becomes unreliable and the results can't be trusted. I would suggest you structure your code in this way:


(if (= 1 a)
 (cond
  ((= (type b) 'ENAME) (get b))
  ((or (= (type b) 'INT) (= (type b) 'REAL)) (+ a b))
 )
)

Message 3 of 7

Kent1Cooper
Consultant
Consultant

@xuantientutungXUK6E wrote:

....

                         ( get b) ; here get b is a text or an object like line, 

                          ( + a b) ; command will error

....


 

How  does b come to be anything like Text or a Line?  You can avoid that kind of problem by simply using the right function to "get b" -- if you use (getreal) or (getint) for it, then b can't  be Text or an object like a Line, because object selection won't even be possible, and non-numerical text inputs will be rejected, and it will ask again until a numerical input is supplied.

Kent Cooper, AIA
Message 4 of 7

john.uhden
Mentor
Mentor

I don't think the proper approach is to catch the errors, but instead to anticipate and prepare for them.

If you are not certain what b is... then first, you should; it's your program.

But that's an excellent idea posted to use the (type) function and the (cond ...) approach.  I almost always try to cover all the bases and if anything is awry, just return nil.

(defun 3+ (n)
  (cond
    ((not (numberp n)) ;; see OR below
      (prompt "\nInput is not a number.") ;;prompt always returns nil
    )
    (+ 3 n)
  )
)

OR, (not (vl-position (type n) '(INT REAL)))

John F. Uhden

Message 5 of 7

xuantientutungXUK6E
Advocate
Advocate

Dear Sir,

i understand what you expressed. 

it can use the condition of choose to do until it return right.

i write the example for easy understand what i try to ask.

is there a way to eat the error in one IF statement (ignore IF statement )  and keep run for another IF statement below.

Thank for your answer.

0 Likes
Message 6 of 7

doaiena
Collaborator
Collaborator

You should probably tell us exactly what you are trying to do, instead of giving a simple example. Post the part of the code that is giving you trouble and explain in detail what the problem is. It's hard to give you good advice when the whole picture isn't clear.

Message 7 of 7

Kent1Cooper
Consultant
Consultant

@xuantientutungXUK6E wrote:

....

is there a way to eat the error in one IF statement (ignore IF statement )  and keep run for another IF statement below.

....


 

Maybe only by making part of the (if) function's test to check whether what it is to work with is appropriate for what it is doing.  Message 2 has one way of doing that, but there could be other approaches.  I agree that more detail about what you are trying to do would be helpful.

 

For some  kinds of things, the (vl-catch-all-apply) function could be useful for you.  >Read about it<.

 

But I repeat, it should be possible to avoid this kind of work-around entirely, by simply ensuring that you get appropriate inputs before  you get to the (if) function in question.

Kent Cooper, AIA