Page 1 of 1

error handling priority

Posted: Fri Oct 07, 2005 8:53 am
by Dmi
What is the priority of "error-event" and "catch"?

How can I implement an analog for such pseudocode?:

Code: Select all

(try body finish-sequence)
Working so that if body rises error or user throws something, "finish-sequence" is evaluated either, but upper function calling this block will still think that user throw or system error is raised in a "body".

Imitating this with (if (not (catch ...)) ... (throw ...)) will affect error-code, I think. And I also want to distinguish between errors and regular throws...

Really, I can live successful without that now. Just a question about programming... :-)

Posted: Fri Oct 07, 2005 12:03 pm
by Lutz
Use the second syntax of catch described in the manual and read also about program flow and error catching here:

http://newlisp.org/DesignPatterns.html#program_flow

and here :

http://newlisp.org/DesignPatterns.html#error_handling

Note, that error-event had some limitations in versions previous to 8.6.7 when using in commandline invoked scripts.

You can nest multiple 'catch'. 'error-event' works only the top level when specifying the error version of 'catch' after 'error-event' then 'cath' will take priority.


Lutz

Posted: Fri Oct 07, 2005 12:40 pm
by Dmi
Oh! Thanks! I forgot about "Design Patterns" :-)
Now it's more clear.

Posted: Fri Oct 07, 2005 7:42 pm
by Lutz
With the second syntax of 'catch' you can easilyt write a (try body else) yourself:

Code: Select all

(define-macro (try body else) 
    (if (not (catch (eval body) 'result)) 
         (eval alternative) 
         result)
)

; now try this

(try (+ 3 4) (error-text)) => 7

(try (xyz) (error-text))  

  => "invalid function in function eval : (xyz)" 
In the body expression you could use 'throw-error' for exceptions you define your self, and 'else' could contain a more elaborate function to treat an error condition.

Lutz