error handling priority

For the Compleat Fan
Locked
Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

error handling priority

Post 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... :-)
WBR, Dmi

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

Oh! Thanks! I forgot about "Design Patterns" :-)
Now it's more clear.
WBR, Dmi

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

Locked