Skip to content

Error Codes ​

Project Links: GitHub · VM Error Source

Overview ​

MaìLang uses the Result type for error handling and does not throw exceptions. Every fallible operation returns Result<T, E> or Option<T>.

Runtime Errors ​

VmError ​

Error codeDescriptionExample
StackOverflowStack overflowRecursion too deep
StackUnderflowStack underflowPop from an empty stack
TypeErrorType error"a" - 1
UndefinedVariableUndefined variableprintln(x)
UndefinedFunctionUndefined functionfoo()
UndefinedPropertyUndefined propertyobj.bar
WrongArgumentCountWrong argument countadd(1)
DivisionByZeroDivision by zero1 / 0
IndexOutOfBoundsIndex out of boundsarr[100]
InvalidOperationInvalid operationnull + 1
RuntimeErrorRuntime errorGeneric error

CompilerError ​

Error codeDescription
UndefinedVariableUndefined variable
TooManyConstantsToo many constants (over 65536)
TooManyLocalsToo many locals (over 256)
TooManyUpvaluesToo many upvalues (over 256)
InvalidAssignmentTargetInvalid assignment target
BreakOutsideLoopbreak outside a loop
ContinueOutsideLoopcontinue outside a loop
ReturnOutsideFunctionreturn outside a function
DuplicateFunctionDuplicate function name
DuplicateClassDuplicate class name
DuplicateTraitDuplicate trait name

ParseError ​

Error codeDescription
UnexpectedTokenUnexpected token
UnexpectedEofUnexpected end of file
InvalidExpressionInvalid expression
InvalidStatementInvalid statement
ExpectedIdentifierExpected identifier
ExpectedTypeAnnotationExpected type annotation
ExpectedExpressionExpected expression
ExpectedPatternExpected pattern
ExpectedBlockExpected block
InvalidAssignmentTargetInvalid assignment target
InvalidFunctionDefinitionInvalid function definition
InvalidClassDefinitionInvalid class definition
InvalidPatternInvalid pattern

LexerError ​

Error codeDescription
UnexpectedCharacterUnexpected character
UnterminatedStringUnterminated string
UnterminatedCharUnterminated char
InvalidEscapeSequenceInvalid escape sequence
InvalidNumberInvalid number format
InvalidUnicodeEscapeInvalid Unicode escape

AnalyzerError ​

Error codeDescription
UndefinedVariableUndefined variable
UndefinedFunctionUndefined function
UndefinedClassUndefined class
UndefinedTraitUndefined trait
TypeMismatchType mismatch
ImmutableAssignmentAssignment to an immutable variable
ConstantAssignmentAssignment to a constant
WrongArgumentCountWrong argument count
MethodNotFoundMethod not found
PropertyNotFoundProperty not found
MissingTraitMethodUnimplemented trait method
CircularInheritanceCircular inheritance
ThisOutsideMethodthis used outside a method
SuperOutsideSubclasssuper used outside a subclass
DuplicateDefinitionDuplicate definition
UnusedVariableUnused variable

Error Handling Best Practices ​

Handle errors with match ​

let result = match divide(10.0, 0.0) {
    Ok(v) => println("结果: {v}"),
    Err(e) => println("错误: {e}")
}

Use the ? operator ​

? is supported: it unwraps Ok/Some and early-returns Err/None from the current function.

fn process() -> Result<str, str> {
    let value = divide(10.0, 0.0)?  // early-return on error
    Ok("处理完成")
}

Custom error types ​

The enum keyword is not supported yet. For now, use string error codes or a class to carry error information:

fn do_something() -> Result<str, str> {
    // ...
    return Err("NotFound: file.txt")
}

Next Steps ​