Error handling

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

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

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.