Error handling
Error handling
Function
error handling is used to actively throw errors in the script and continue execution after being caught in the outer layer. throw immediately interrupts the current flow of execution until caught by the nearest catch; if there are no catchs, the program terminates with an uncaught exception.
Syntax
try { // Codes that may throw errors } catch e { // e is the error message } throw 'fatal'
Parameters
-
try: Wrap the code block that needs to catch the error. -
catch e: Catch errors thrown intry, whereeis the name of the variable that receives the error message. -
throw value: Throws the value of any expression. A common usage is to throw a string error message.
Return Value
try/catch can also be used as an expression. When no error is thrown, the value of the last statement of the try code block is returned; when an error is caught, the value of the last statement of the catch code block is returned.
Code Examples
result = try { throw 'fatal' 'ok' } catch e { 'caught: ' + e } // Output: catch: fatal print result
Notes
- The sibling code following
throwwill not continue to execute.
throw can propagate outward through function calls until captured by the outer catch.
-
catchvariables are treated as ordinary variable variables; currently BT does not have block-level scope, so do not reuse confusing variable names in the same scope.
throw terminates the program with an uncaught exception message.