您可以指定如何捕获特定错误,例如errno.ENAMETOOLONG:
具体到你的问题...
try:
# try stuff
except OSError as oserr:
if oserr.errno != errno.ENAMETOOLONG:
# ignore
else:
# caught...now what?
具体到你的评论...
try:
# try stuff
except Exception as err:
# get the name attribute from the exception class
errname = type(err).__name__
# get the errno attribute from the exception class
errnum = err.errno
if (errname == 'OSError') and (errnum == errno.ENAMETOOLONG):
# handle specific to OSError [Errno 36]
else if (errname == 'ExceptionNameHere' and ...:
# handle specific to blah blah blah
.
.
.
else:
raise # if you want to re-raise; otherwise code your ignore
这将抓取所有由try
. 然后它检查是否__name__
匹配任何特定异常以及您要指定的任何附加条件。
您应该知道,except
如果遇到错误,除非您指定具体的异常,否则无法绕过。