Ric*_*kyA 0 python exception-handling
我尝试构建一个except子句来捕获除 [原文如此] 特定类型异常之外的所有内容:
try:
    try:
        asdjaslk
    except not NameError as ne: #I want this block to catch everything except NameError
        print("!NameError- {0}: {1}".format(ne.__class__, ne))
except Exception as e: #NameError is the only one that should get here
    print("Exception- {0}: {1}".format(e.__class__, e))
语言接受not中except条款,但什么都不做吧:
>>> Exception- <type 'exceptions.NameError'>: name 'asdjaslk' is not defined
是否有可能做到这一点,或者我应该重新做raise他们?
你将不得不重新加注。一条except语句只能列入白名单,不能列入黑名单。
try:
    asdjaslk
except Exception as ne:
    if isinstance(ne, NameError):
        raise
该not NameError表达式返回的False,所以你基本上是试图赶上:
except False:
但没有例外会匹配布尔实例。
语法允许任何有效的 Python 表达式,并且抛出的异常与该表达式的结果相匹配。except SomeException if debug else SomeOtherException:例如,是完全有效的。