Hetu Script Language
Guide
Grammar
  • Dart APIs
  • Hetu APIs
  • VS Code extension
Github
  • English
  • 简体中文
  • English
Guide
Grammar
  • Dart APIs
  • Hetu APIs
  • VS Code extension
Github
  • English
  • 简体中文
  • English
  • Introduction
  • Identifier & keywords
  • Builtin types
  • Operators
  • Variable
  • Control flow
  • Function
  • Enum
  • Class
  • Struct
  • Type system
  • Future, async & await
  • Import & export
  • Strict mode
  • Assert & error

Error & exception

You can manually throw a Dart exception within the script using the assert or throw keyword.

assert

assert(1 > 5)

Will throw an AssertionError. The error message will contain the source code text in the parentheses to help you understand why it failed.

The expression within the parentheses must be a boolean value.

assertions can be stripped from compiled bytecode by setting removeAssertion: true in the Hetu config.

throw

var i = 42
throw 'i is ${i}!'

Will throw a script throws error. The error message will contain the toString() value of the expression after the keyword.

You must provide an expression after throw, although the value may be null.

Common runtime errors

The interpreter reports errors with source location information (file, line, column) when processError is enabled in the config. Common runtime errors include:

Error codeCause
undefinedAccessing a variable or identifier that has not been declared
notInitializedAccessing a late variable before its first assignment
immutableReassigning a final, const, or late variable
arityCalling a function with the wrong number of positional arguments
argTypePassing an argument of the wrong type (when runtime type checking is enabled)
notCallableAttempting to call a non-function value
undefinedMemberAccessing a non-existent member on a class instance
outOfRangeList index out of bounds
typeCastInvalid type cast with as operator
circleInitVariable initialization references itself
notSpreadableObjSpreading a non-iterable, non-struct value
callNullObjectAttempting to call a method on null
subGetKeyInvalid subscript key type

Error handler configuration

The InterpreterConfig provides several options for controlling error output:

  • processError (default: true): When enabled, errors are augmented with source file, line, and column information before being re-thrown.
  • showHetuStackTrace (default: false): Include the Hetu script call stack in error output.
  • showDartStackTrace (default: false): Include the Dart host call stack in error output.
  • stackTraceDisplayCountLimit (default: 5): Maximum number of stack frames to display.

Error handling

The script does not support try...catch functionality. It is recommended to handle errors in Dart code rather than within the script.

Last Updated: 4/30/26, 3:22 AM
Contributors: hythloday@live.com, 邵燃, Ran Shao, chengfubeiming
Prev
Strict mode