簡體   English   中英

python中的try-except方法出錯

[英]error with try-except method in python

我在使用 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

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")

也許使用except IOError as e:或簡單地使用except:以確保它捕獲錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM