# 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 `catch`s, the program terminates with an uncaught exception. ## Syntax ```bt 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 in `try`, where `e` is 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 ```bt result = try { throw 'fatal' 'ok' } catch e { 'caught: ' + e } // Output: catch: fatal print result ``` ## Notes - The sibling code following `throw` will not continue to execute. - `throw` can propagate outward through function calls until captured by the outer `catch`. - `catch` variables 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. - An uncaught `throw` terminates the program with an uncaught exception message.