<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>异常 on 老张开工了</title>
    <link>/%E5%BC%82%E5%B8%B8/</link>
    <description>Recent content in 异常 on 老张开工了</description>
    <generator>Hugo</generator>
    <language>zh-cn</language>
    <lastBuildDate>Thu, 14 May 2026 00:00:00 +0000</lastBuildDate>
    <atom:link href="/%E5%BC%82%E5%B8%B8/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>异常处理与调试</title>
      <link>/posts/2026/05/exceptions-debugging/</link>
      <pubDate>Thu, 14 May 2026 00:00:00 +0000</pubDate>
      <guid>/posts/2026/05/exceptions-debugging/</guid>
      <description>程序总会有出错的时候——用户输入了无效数据、网络连接中断、磁盘空间不足……优秀的程序员不是写出永不报错的代码（这是不可能的），而是能优雅地处理错误，让程序在异常情况下也能行为可预测、可排查。&#xA;本章将系统学习 Python 的异常处理机制——从 try/except 到自定义异常，再到使用 pdb 和 logging 进行调试，让你的代码从&amp;quot;动不动就崩溃&amp;quot;变成&amp;quot;稳如磐石&amp;quot;。&#xA;Python 的异常哲学 Python 推崇 EAFP 原则——&amp;ldquo;Easier to Ask for Forgiveness than Permission&amp;rdquo;（请求原谅比获得许可更容易）。也就是：先尝试执行，如果出错了再处理，而不是在动手之前做各种检查。&#xA;# LBYL 风格（Look Before You Leap）——非 Python 方式 def safe_divide_lbyl(a, b): if b == 0: return None if not isinstance(a, (int, float)): return None return a / b # EAFP 风格——Python 方式 def safe_divide_eafp(a, b): try: return a / b except (ZeroDivisionError, TypeError): return None try/except/else/finally Python 的异常处理结构由四个子句组成：</description>
    </item>
  </channel>
</rss>
