Creating and catching errors

In a situation where you know a runtime error could occur, you can invoke a specific error yourself by using cause-error. For example:

m: 10if n = 0 [ cause-error 'math 'zero-divide [] ]print m / n

This line generates a *** Math Error: attempt to divide by zero when n is 0. This could be useful when entering a function with an n parameter that could be 0, and when we want to divide by  in the function.

Note that it needs the literal words 'math and 'zero-divide, describing the error's type and name; the third argument is a parameter block, which can be empty. Instead of cause-error, you can also encounter make error! [math zero-divide] or even make error! 400. They all do the same—generate the error with that ...

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.