我遇到了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
运行代码后出现错误
我得到了这个:
IOError:[Errno 2]没有这样的文件或目录:' \\ ntsrv1 \ tohna \ SecurityTeam \ Varonis \ Varonis_monitoring_report \ Varonis_Action_Report.csv'
并且它没有转到我需要收到错误的邮件的部分,它只是停止
谁知道为什么?TNX
答案 0 :(得分:0)
except ValueError as e:
print("ValueError caught: " + str(e))
只捕获ValueError
类型的例外。异常倾向于从Exception
类派生,因此这将捕获任何正常异常:
except Exception as e:
print("Exception caught:" + str(e))
为了确保你能抓住所有东西,你可以使用:
except:
print("Unknown exception caught")
答案 1 :(得分:0)
也许使用except IOError as e:
或简单except:
来确保它捕获错误