1. 在整個跑code 的過程 (life cycle) 遇到的問題 大致可以分三類:

    1. 語法錯誤 syntax error
    2. 邏輯錯誤 (logic mistake : bug)
    3. 例外處理(exception) 1.
  2. 處理exception approach:

    1. LBYL (look before you leap) → if else
    2. EAFP(easier to ask forgiveness than permission) → try - except
      1. 有點try and error 的味道,先試run 有問題再ask for permission (寫exception code)
    3. python 主要是用 EAFP
  3. LBYL 大部分的程式碼多是在做 error handling → low readability 低可讀性

  4. EAFP 程式 因為都放到 try 去跑 執行速度比較快

  5. EAFP → reduce race conditions 機率

  6. EAFP → 可以減少check file 和 execute file 中間的時間差 避免file crash

  7. LBYL (preferable) 的用法

    1. try 一連串的程試碼很花時間
    2. important task (無法回溯) 先確保condition 都OK
  8. coding 極大多數的時間是在處理 客戶用到的exception 情況

Untitled

Untitled

Untitled

11.5. 日誌記錄 (Logging)

logging 模組提供功能齊全且富彈性的日誌記錄系統。在最簡單的情況下,日誌訊息會被發送到檔案或 sys.stderr

**import** **logging**logging.debug('Debugging information') logging.info('Informational message') logging.warning('Warning:config file **%s** not found', 'server.conf') logging.error('Error occurred') logging.critical('Critical error -- shutting down')

Logging HOWTO - Python 3.10.6 documentation