Handling errors
Errors can be caused by wrong input from the user or they can be produced by the system.
If you enter 4 / 0
in the REPL, you will get a *** Math Error: attempt to divide by zero
alert. The Red console does not crash in the case of an error, but a compiled program will.
Note
The different types of predefined errors can be found with ? system/catalog/errors
.
To see the specific errors, use the type as a refinement—for example,? system/catalog/errors/syntax
, which shows, for example, a no-header
, missing
, or invalid
error.
Now, let's return to our guessing game. What happens if the user enters a string such as "abc"
instead of a number? Then the guessing stops right there—the program crashes with the error:
;-- see Chapter04/handling-errors.red: *** Script Error: cannot compare 4 with "abc" *** Where: <
This occurs when comparing the secret number (in this case, 4
) with the string 4 < "abc"
.
Red has different ways to make your program resilient against runtime errors that are...