Error Class
Error is Alia's built-in error class, used to represent and handle error information in programs.
Creating Errors
Using the Constructor
The constructor accepts (message, name, cause) by position, and array-style calls pass arguments in the order of the formal parameters.
const cause = new Error ['Cause']
const err1 = new Error ['Something went wrong']
const err2 = new Error ['Error message', 'CustomError']
const err3 = new Error ['Error message', 'CustomError', cause]
Instance Properties
message
The error's descriptive message.
const err = new Error ['Invalid input']
print err.message ~ outputs 'Invalid input'
name
The error's name/type.
const err = new Error ['Error', 'TypeError']
print err.name ~ outputs 'TypeError'
cause
The error's underlying cause. Defaults to Null.
const originalError = new Error ['Original error']
const wrappedError = new Error ['Wrapped error', 'Error', originalError]
print wrappedError.cause ~ outputs the original error object
stack
The error's stack information. Taken from the cause's stack; Null when there is no cause.
const err = new Error ['Error occurred']
print err.stack ~ outputs null
Instance Methods
toString
Converts the error to its string representation. Zero-argument methods are invoked via call, returning a String of the form Error <name>: <message>.
const err = new Error ['Invalid input', 'TypeError']
print call err.toString ~ outputs 'Error TypeError: Invalid input'
valueOf
Returns the error's message content. Zero-argument methods are invoked via call.
const err = new Error ['Invalid input']
print call err.valueOf ~ outputs 'Invalid input'
Error Handling
throw Statement
Throws an error:
throw new Error ['Something went wrong']
throw new Error ['Invalid type', 'TypeError']
try-catch Statement
Catches and handles errors:
try {
throw new Error ['Error occurred']
} catch err {
print 'Caught error: ' .. (call err.toString)
}
Error Type Examples
Custom Error Types
const typeError = new Error ['Expected a number', 'TypeError']
const rangeError = new Error ['Value out of range', 'RangeError']
const validationError = new Error ['Invalid input', 'ValidationError']
Error Chains
const rootCause = new Error ['Database connection failed']
const serviceError = new Error ['Service unavailable', 'ServiceError', rootCause]
const apiError = new Error ['API request failed', 'APIError', serviceError]
print 'API error cause: ' .. (call apiError.cause.toString)
Example Code
const simpleError = new Error ['Simple error']
const namedError = new Error ['Named error', 'CustomError']
print 'Simple error: ' .. (call simpleError.toString)
print 'Named error: ' .. (call namedError.toString)
print 'Error message: ' .. namedError.message
print 'Error name: ' .. namedError.name
try {
throw new Error ['Thrown error']
} catch err {
print 'Caught error: ' .. (call err.toString)
}
alias validateInput <String input> -> {
if input.length == 0 {
throw new Error ['Input cannot be empty', 'ValidationError']
}
true
}
try {
validateInput ''
} catch err {
if err.name == 'ValidationError' {
print 'Validation error: ' .. err.message
} else {
print 'Other error: ' .. (call err.toString)
}
}
const original = new Error ['Root cause']
const wrapped = new Error ['Wrapped error', 'WrappedError', original]
print 'Outer error: ' .. (call wrapped.toString)
if Ok wrapped.cause {
print 'Inner cause: ' .. (call wrapped.cause.toString)
}
try {
const value = -1
if value < 0 {
throw new Error ['Value must be positive', 'RangeError']
}
} catch err {
switch err.name {
case 'RangeError' {
print 'Range error: ' .. err.message
}
case 'TypeError' {
print 'Type error: ' .. err.message
}
default {
print 'Unknown error: ' .. (call err.toString)
}
}
}