有没有什么方法可以将用户定义的异常(我们的自定义异常)存储在列表中?因此,如果发生任何其他不在列表中的异常。。程序应该简单地中止。
最佳答案
单个except
可能有多个错误,自定义或其他:
>>> class MyError(Exception):
pass
>>> try:
int("foo") # will raise ValueError
except (MyError, ValueError):
print "Thought this might happen"
except Exception:
print "Didn't think that would happen"
Thought this might happen
>>> try:
1 / 0 # will raise ZeroDivisionError
except (MyError, ValueError):
print "Thought this might happen"
except Exception:
print "Didn't think that would happen"
Didn't think that would happen