-
-
Save stilllisisi/418da2d7ce70cb1ce4d91b7204029381 to your computer and use it in GitHub Desktop.
【python-异常处理】忽略异常的处理
This file contains 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