简体   繁体   English

捕获导致我的Python脚本停止执行的错误

[英]Catch the error which stops the execution of my Python script

I am trying to catch the error that stops the execution of my python script. 我正在尝试捕获导致我的python脚本停止执行的错误。 But I don't want to catch all the errors or exception because some of them are not affecting the success of my scripts they are just guidance or console Info. 但是我不想捕获所有的错误或异常,因为其中一些并不影响脚本的成功,它们只是指导或控制台信息。 I only want to catch and stock in a variable the error that cause my python script to stop. 我只想捕获并存储一个导致我的python脚本停止的错误的变量。 Thanks by advance 预先感谢

Did you try: 你试过了吗:

try:
   code where my error occurs
except:
   pass

Here's a small example on how to catch a specific error. 这是一个有关如何捕获特定错误的小示例。 Let's say you would want to check if a ZeroDivisionError is raised: 假设您要检查是否引发ZeroDivisionError

a = 3
b = 0

try:
    _ = a/b
except ZeroDivisionError as e:
    print(str(e))

Running the above code snippet would result in: division by zero 运行上面的代码片段将导致: division by zero

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM