知识卡片
捕获崩溃的边界
catch_unwind只捕获采用展开机制的panic,遇到abort无法恢复;目标与编译设置可能改变行为。它也不会把中断的多步更新自动回滚到一致状态。普通可失败操作应优先Result,跨语言需要温和返回错误时再设计捕获边界。
边界依据:catch_unwind官方文档,2026-10-27查阅。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
- · 捕获崩溃的边界
原书出处 · 1 处
- Chapter 3. Concepts Item 18: Don’t panic · 查看原书依据
catch_unwind as a way to simulate exceptions, because it appears to provide a mechanism for catching panics at a point further up the call stack. Consider a function that panics on an invalid input: fn divide(a: i64, b: i64) -> i64 { if b == 0 { panic!("Cowardly refusing to divide by zero!"); } a / b } Trying to invoke this with an invalid input fails as exp
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第3章《Item 18: Don’t panic》(spine7;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch03.html;u106-ch03-043)。
- 结论依据:原文在“catch_unwind”附近的定义、例子或代码支撑本卡核心说明:catch_unwind只捕获采用展开机制的panic,遇到abort无法恢复;目标与编译设置可能改变行为。边界按已读前提解释;示例未执行。
- 原始内容:catch_unwind as a way to simulate exceptions, because it appears to provide a mechanism for catching panics at a point further up the call stack. Consider a function that panics on an invalid input: fn divide(a: i64, b: i64) -> i64 { if b == 0 { panic!("Cowardly refusing to divide by zero!"); } a / b } Trying to invoke this with an invalid input fails as exp