[英]error with try-except method in python
I am having a problem with the try-except method i would like to monitor any errors in my script by sending those errors to my mail the script only checks file existence and it looks like:我在使用 try-except 方法时遇到问题,我想通过将这些错误发送到我的邮件来监视脚本中的任何错误,脚本只检查文件是否存在,它看起来像:
def check_file_existence():
try:
with open('\\\\ntsrv1\\tohna\\SecurityTeam\\Varonis\\Varonis_monitoring_report\\Varonis_Action_Report.csv', 'r') as temp_file:
temp_file.close()
except ValueError as e:
e = str(e)
print(e)
status_mail_notofication('error in move_report_to_folder_adding_date_to_file Function under varonis_report_analysis script ','there was an error in check_file_existence Function','security88876@gmail.com')
sys.exit(0)
return
after running the code with purpose to get an error故意运行代码后出现错误
i am getting this:我得到这个:
IOError: [Errno 2] No such file or directory: '\\\\ntsrv1\\tohna\\SecurityTeam\\Varonis\\Varonis_monitoring_report\\Varonis_Action_Report.csv' IOError: [Errno 2] 没有这样的文件或目录:'\\\\ntsrv1\\tohna\\SecurityTeam\\Varonis\\Varonis_monitoring_report\\Varonis_Action_Report.csv'
and it doesn't go to the except part where i need to get the mail with the error ,it just stops并且它不会转到我需要接收带有错误的邮件的除外部分,它只是停止
anyone know why?有谁知道为什么?
TNX TNX
except ValueError as e:
print("ValueError caught: " + str(e))
Just catches exceptions of type ValueError
.只捕获
ValueError
类型的异常。 Exceptions tend to be derived from the Exception
class, so this would catch any normal exception:异常往往是从
Exception
类派生的,所以这会捕获任何正常的异常:
except Exception as e:
print("Exception caught:" + str(e))
To be entirely sure you're catching everything, you can use:为了完全确定你捕捉到了一切,你可以使用:
except:
print("Unknown exception caught")
也许使用except IOError as e:
或简单地使用except:
以确保它捕获错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.