知识卡片
错误传播保留调用者决定
底层功能通过返回 Result 保留具体的成功与失败信息,让更了解用户目标的调用者决定如何处理。读取用户名的函数可上交文件缺失或读取失败。上层若有替代来源,便可继续读取;若当前流程依赖用户名,也可据错误停止。
相关卡片 [[结果类型承载失败信息]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 错误处理 Propagating Errors · 查看原书依据
The code that calls this code will then handle getting either an Ok value that contains a username or an Err value that contains an io::Error. It’s up to the calling code to decide what to do with those values. If the calling code gets an Err value, it could call panic!
参考来源
- 位置:《The Rust Programming Language(第2版)》第09章 错误处理(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c09.xhtml`,块 0089)
- 结论依据:原文提供多种上层策略,并解释底层为什么没有足够上下文替它决定。
- 原始内容:The code that calls this code will then handle getting either an Ok value that contains a username or an Err value that contains an io::Error. It’s up to the calling code to decide what to do with those values. If the calling code gets an Err value, it could call panic!