Try / Catch
Handling expected errors and dealing with the result.
The try/catch section is designed to handle expected errors and prevent the program terminating, while also allowing special fail-case behaviour to be run.
It forms a tree structure of two possible branches. One or both of these may be run.
1
try:
2
// attempted branch
3
catch %Variable%:
4
// failure branch
Copied!

Try Block

These must always start with a try effect as the section header. This header has no inputs or options.
1
try:
2
print "yes"
Copied!
The code in this block will always be attempted. However, it is run with the expectation of failing (an error being thrown.)
The try section must always be followed by a catch section.

Catch Block

These must always follow a try section. The catch %Variable% section accepts a normal variable.
The error will be stored in this variable.
1
try:
2
print "always run"
3
catch {var}:
4
print "caught the error: " + {var}
5
print "continues here"
Copied!
The code in this block will be run only if the preceding try-section fails. If no error is thrown, the catch section will not be run.
The catch section will be jumped to after the line that fails
1
try:
2
print "hello"
3
assert 1 is 2 // error! jump -> catch
4
print "never run" // this will not be reached.
5
catch {var}: // {var} contains the error object
6
print "caught an error: " + {var}
7
print "continues here"
Copied!
If no error occurs, the catch-section will be skipped.
1
try:
2
print "always run"
3
catch {var}:
4
print "never run"
5
print "continues here"
Copied!
Any break instructions will exit this tree entirely (jump to after the catch section finishes.)
1
try:
2
print "always run"
3
break section // exits all
4
print "never run"
5
catch {var}: // skipped by the break
6
print "never run"
7
print "continues here"
Copied!
Last modified 3mo ago
Copy link