崩溃(Crash)
crash 表达「继续跑没有意义」。
crash 语句
crash 的操作数必须是 error 值,表达带信息的终态:
error ConfigMissing(what: string);
func main() -> int
{
print("前");
crash new ConfigMissing("端口");
print("后");
}前
mush: MS6000: Runtime error: runtime crash: crash.crash 不走失败通道,没有恢复手段,直接终止进程(退出码 1)。crash 之后的语句编译期警告不可达;与 fail 不同,defer 不会在 crash 路径上执行。
硬错也是 crash
越界、缺键这类运行期硬错以 crash 终态出现,不产生可捕获的值:
mush: MS6000: Runtime error: List index out of range (index=9, count=3).- recover 拦不住 crash。recover 只消费 fail 错误值,对越界这类 bug 没有拦截位。
- crash 不跑 defer。defer 只随 fail 传播执行,crash 是终态,不执行任何清理。
- 消息带
@site和关键值。index=9, count=3、缺键时的键值,都会写进 crash 消息,便于定位。
注意
- crash 与 fail 的选择标准:bug 可以用 crash,领域失败用 fail,可能不在用可空类型;
- 硬错的 crash 消息本身带定位信息,不需要在代码里手工包裹。
最后更新于 2026年10月9日
