← 返回起始笔记位置

知识卡片

普通读书笔记卡 · 6074.d.1.a.3.c.12.a.1.g · 生成模型:GPT-6

异常增加隐式控制路径

理解等级:高级

要推断一段代码怎样执行,需要考虑从入口到结果的所有分支。普通条件把分支写在当前位置,抛异常则可能跳过当前函数余下代码,并穿过数层调用直到某处捕获。依赖行为越多,越难只看本地代码就找到错误处理路径。把常规失败编码为返回值能把失败边界暴露在调用处。

相关卡片 [[以返回值表示失败]]、[[Result与panic的边界]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • From panic to result: Error handling 错误带来的控制路径 · 查看原书依据

      Exceptions are a cause of cyclomatic complexity: if something can throw an exception, that is yet another path that your code might take. What makes them worse than conditionals and loops is how totally unexpected their behavior can be. In a large codebase, with a core language that likes throwing exceptions, plus a lot of dependencies, almost anything you call might throw an error in the right (well, wrong) circumstances.

    参考来源

    - 位置:《Write Powerful Rust Macros》第07章《From panic to result: Error handling》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-7.xhtml#p16) - 结论依据:7.1 先用条件语句解释路径增多,再用 Java Optional.of 的例子指出依赖调用可能突然沿调用栈转交给 catch。 - 原始内容:Exceptions are a cause of cyclomatic complexity: if something can throw an exception, that is yet another path that your code might take. What makes them worse than conditionals and loops is how totally unexpected their behavior can be. In a large codebase, with a core language that likes throwing exceptions, plus a lot of dependencies, almost anything you call might throw an error in the right (well, wrong) circumstances.