跳到主要内容

Error 错误类

Error 是 Alia 的内置错误类,用于表示和处理程序中的错误信息。

创建错误​

使用构造函数​

构造函数按位置接收 (message, name, cause),数组式调用按形参顺序传参。

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]

实例属性​

message​

错误的描述信息。

const err = new Error ['Invalid input']
print err.message ~ 输出 'Invalid input'

name​

错误的名称/类型。

const err = new Error ['Error', 'TypeError']
print err.name ~ 输出 'TypeError'

cause​

错误的原始原因。缺省为 Null。

const originalError = new Error ['Original error']
const wrappedError = new Error ['Wrapped error', 'Error', originalError]
print wrappedError.cause ~ 输出原始错误对象

stack​

错误的堆栈信息。取自 cause 的堆栈,无 cause 时为 Null。

const err = new Error ['Error occurred']
print err.stack ~ 输出 null

实例方法​

toString​

将错误转换为字符串表示。无参数方法经由 call 调用,返回形如 Error <name>: <message> 的 String。

const err = new Error ['Invalid input', 'TypeError']
print call err.toString ~ 输出 'Error TypeError: Invalid input'

valueOf​

返回错误的消息内容。无参数方法经由 call 调用。

const err = new Error ['Invalid input']
print call err.valueOf ~ 输出 'Invalid input'

错误处理​

throw 语句​

抛出错误:

throw new Error ['Something went wrong']
throw new Error ['Invalid type', 'TypeError']

try-catch 语句​

捕获和处理错误:

try {
throw new Error ['Error occurred']
} catch err {
print '捕获到错误: ' .. (call err.toString)
}

错误类型示例​

自定义错误类型​

const typeError = new Error ['Expected a number', 'TypeError']
const rangeError = new Error ['Value out of range', 'RangeError']
const validationError = new Error ['Invalid input', 'ValidationError']

错误链​

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 错误原因: ' .. (call apiError.cause.toString)

示例代码​

const simpleError = new Error ['Simple error']
const namedError = new Error ['Named error', 'CustomError']
print '简单错误: ' .. (call simpleError.toString)
print '命名错误: ' .. (call namedError.toString)
print '错误消息: ' .. namedError.message
print '错误名称: ' .. namedError.name
try {
throw new Error ['Thrown error']
} catch err {
print '捕获错误: ' .. (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 '验证错误: ' .. err.message
} else {
print '其他错误: ' .. (call err.toString)
}
}
const original = new Error ['Root cause']
const wrapped = new Error ['Wrapped error', 'WrappedError', original]
print '外层错误: ' .. (call wrapped.toString)
if Ok wrapped.cause {
print '内层原因: ' .. (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 '范围错误: ' .. err.message
}
case 'TypeError' {
print '类型错误: ' .. err.message
}
default {
print '未知错误: ' .. (call err.toString)
}
}
}

下一步​