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.

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: ...

Get Learn Red - Fundamentals of Red now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.