Exceptions

interface Exceptions : Variable

脚本层统一异常处理入口

DSL 风格 提供 try-catch-finally、手动抛异常、构造脚本层 Throwable 对象等功能。 该组件作为 全局变量 注入脚本,名称固定为 exceptions,必须在脚本中通过 exceptions.xxx 显式调用。

线程安全

所有方法均无副作用,可在任意协程/线程安全调用。

示例

// 1. try-catch 返回值
val result: String? = exceptions.tryCatch {
tryBlock { riskyCall() }
catchBlock{ e -> log(e.message) }
finally { cleanup() }
}

// 2. 主动抛异常
exceptions.throwException {
setMessageAndClassName("参数非法", "java.lang.IllegalArgumentException")
}

// 3. 构造脚本层 Throwable 对象(不抛出)
val thr = exceptions.newThrowable {
setThrowable(java.io.IOException("IO 错误"))
}

Author

M8Test, [email protected], https://m8test.com

Since

2022-08-25

Functions

Link copied to clipboard
abstract fun getGlobalName(): String

获取作为全局变量时的名称.

Link copied to clipboard
abstract fun getPublicType(): Type

获取 Variable 注入到脚本时需要显示的类型.

Link copied to clipboard
abstract fun isPrefixRequired(): Boolean

是否需要添加前缀, 每种编程语言添加的前缀不一样, 前缀可以通过 Language.getVariablePrefix 获取.

Link copied to clipboard
abstract fun isSuffixRequired(): Boolean

是否需要添加后缀, 每种编程语言添加的后缀不一样, 后缀可以通过 Language.getVariableSuffix 获取.

Link copied to clipboard
abstract fun newThrowable(exceptionBuilder: ExceptionBuilder.() -> Unit): Throwable

构造一个 脚本层 Throwable 对象(不抛出)。

Link copied to clipboard
abstract fun throwException(exceptionBuilder: ExceptionBuilder.() -> Unit)

立即抛出一个由 ExceptionBuilder 构建的 Java 异常

Link copied to clipboard
abstract fun <T> tryCatch(config: ExceptionCatcher<T>.() -> Unit): T?

配置并执行一次 try-catch-finally 块。