Created
August 1, 2019 13:07
-
-
Save banjin/23a21a0be4e22c49b7bf419ec8b62a7b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
忽略抛出的异常: | |
常用的写法: | |
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