Skip to content

Instantly share code, notes, and snippets.

@banjin
Created August 1, 2019 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save banjin/23a21a0be4e22c49b7bf419ec8b62a7b to your computer and use it in GitHub Desktop.
Save banjin/23a21a0be4e22c49b7bf419ec8b62a7b to your computer and use it in GitHub Desktop.
忽略抛出的异常:
常用的写法:
try:
os.remove('somefile.tmp')
except OSError:
pass
可以写成:
with ignored(OSError):
os.remove('somefile.tmp')
python2 的写法:
@contextmanager
def ignored(*exceptions):
try:
yield
except exceptions:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment