Error handling



function!  attempt  Red-by-example    MyCode4fun

Evaluates a block and returns the result or none if an error occur.

>> attempt [a: 10 b: 9]        ;first lets try with no errors...
== 9

>> a
== 10                                ;... no problems here!

>> attempt [a: 10 nosyntax]        ;nosyntax has no value: ERROR!
== none

>> attempt [divide 100 0]
== none

native! try  Red-by-example    MyCode4fun

Tries to evaluate a block. Returns the value of the block, but if an error! occurs, the block is abandoned, and an
error value is returned.
To identify a block that generates an error without actually having the error output printed, we use the function error?.

You may ask why not use attempt instead of error? & try. I think the answer is that the error? & try combination returns true and false, instead of none or an evaluation. This is useful when used inside other structures.

>> error? [nosyntax]
== false                        ;nosyntax has no value and it generates an error,
                               ;but only if evaluated. In itself, is not a error! datatype.

>> try [nosyntax]
*** Script Error: nosyntax has no value
*** Where: try
*** Stack:                ; just "try" does not work, you get an error!!

>> error? try [nosyntax]
== true                        ;OK!

>> error? try [divide 100 0]
== true

native! catch and native! throw  Red-by-example  

These are used to handle errors, but I could not figure how. Does not seem to be a beginner's issue.


< Previous topic                                                                                          Next topic >