簡體   English   中英

使用異常處理防止系統崩潰時出錯

[英]error when using exception handling to prevent system from crashing

我正在嘗試使用異常處理來防止我的系統崩潰,我使用了下面的代碼,其中這個 csv 文件實際上並不存在。 但是即使系統崩潰了。 有人可以幫我更正我的代碼嗎....請幫幫我

這些是我的源代碼:

while True:
    try:
        import pandas as pd  # data processing, csv file I/O(e.g pd.read_csv)

        df = pd.read_csv('C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv')
        print(df)
    except Exception as err:
            print("Uh oh, please send me this message: '" + err + "'")

我遇到的錯誤

Traceback (most recent call last):
  File "C:/Users/User/PycharmProjects/EmotionAnalysis/abcd.py", line 5, in <module>
    df = pd.read_csv('C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv')
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\io\parsers.py", line 685, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\io\parsers.py", line 457, in _read
    parser = TextFileReader(fp_or_buf, **kwds)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\io\parsers.py", line 895, in __init__
    self._make_engine(self.engine)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\io\parsers.py", line 1135, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\io\parsers.py", line 1917, in __init__
    self._reader = parsers.TextReader(src, **kwds)
  File "pandas/_libs/parsers.pyx", line 382, in pandas._libs.parsers.TextReader.__cinit__
  File "pandas/_libs/parsers.pyx", line 689, in pandas._libs.parsers.TextReader._setup_parser_source
FileNotFoundError: [Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/User/PycharmProjects/EmotionAnalysis/abcd.py", line 8, in <module>
    print("Uh oh, please send me this message: '" + err + "'")
TypeError: must be str, not FileNotFoundError

Process finished with exit code 1

您不能將異常連接到字符串,您可以這樣做:

print("Uh oh, please send me this message: '{}'".format(err))

暫無
暫無

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

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