Skip to content

API Error

Liu.Yandong.Hanks edited this page Aug 19, 2026 · 3 revisions

Error

脚本错误值

Script error value

脚本 API 目录

Script API Directory

概述

Overview

Error 用于创建可 throw 的错误值。运行时会保留错误消息和脚本调用栈,适合向调用方说明无法继续执行的条件。

Error creates values that can be thrown. The runtime retains the message and script call stack, making it suitable for reporting a condition from which the caller cannot continue.

new Error(message)

Parameters:

  • message
    描述错误原因的字符串。

    String describing the failure.

Returns

可抛出的 Error 值。

A throwable Error value.

func requirePositive(value) {
    if (value <= 0) throw new Error("value must be positive");
    return value;
}

return requirePositive(3);

边界行为

仅在确实无法恢复时抛出;预期的“未找到”结果通常更适合返回 null 或约定值。

Throw only for unrecoverable conditions; an expected "not found" result is usually better represented by null or an agreed value.

Clone this wiki locally