Return to homepage

DOSATO

Copyright © 2024 Sebastiaan Heins

1.12 Error handling

catch

catch is an extension.
catch is used to catch errors.
catch is followed by a block of code.
If an error occurs in the previous block of code, the error is caught and the block of code is executed. It prevents the program from crashing
Catch encapsulates everything before it, so it can be used to catch errors in a larger block of code.
Example of a catch statement:

do {
    do say (notDefined) // Error is thrown
} catch {
    do say ("An error occurred") // Error is caught
    do say ("The code: " + _) // The code is stored in the _ variable
}
Info!
The _ variable is a special variable that stores the error message.
More info about the _ variable can be found in the 2.4 The _ variable