Effective Python 读书笔记 Item 13 Take Advantage of Each Block in try/except/else/finally

Use try/except/else/finally when you want to do it all in one compound statement.

Effective Python 读书笔记 Item 13 Take Advantage of Each Block in try/except/else/finally

  1. The try/finally compound statement lets you run cleanup code regardless of whether exceptions were raised in the try block.
  2. The else block helps you minimize the amount of code in try blocks and visually distinguish the success case from the try/except blocks.
  3. An else block can be used to perform additional actions after a successful try block but before common cleanup in a finally block.