Skip to main content
 首页 » 编程设计

python之python 中的异常未知错误

2025年05月04日13zhengyun_ustc

      except ValueError: 
        print "the input is Invaild(dd.mm.year)" 
    except as e: 
        print "Unknown error" 
        print e 

这是我写的代码,如果一个不同的错误会发生 valueerror 它会在 e 中打印吗? 谢谢

请您参考如下方法:

您需要在此处捕获 BaseException 或 object 才能分配给 e:

except ValueError: 
    print "the input is Invaild(dd.mm.year)" 
except BaseException as e: 
    print "Unknown error" 
    print e 

或者,更好的是,Exception:

except ValueError: 
    print "the input is Invaild(dd.mm.year)" 
except Exception as e: 
    print "Unknown error" 
    print e 

毯子 except: 将捕获与 BaseException 相同的异常,仅捕获 Exception 将忽略 KeyboardInterrupt、SystemExit 和 GeneratorExit。不捕捉这些通常是个好主意。

有关详细信息,请参阅 exceptions documentation .