使用异常处理以防止系统崩溃时出现几行

时间:2020-04-29 09:37:51

标签: python python-3.x exception error-handling

我正在尝试使用异常处理来防止系统崩溃,因此我在实际不存在该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: '{}'" .format(err))

结果:

Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
..................

1 个答案:

答案 0 :(得分:0)

赶上期望意味着您的系统不会退出并显示相应的错误代码。相反,它会执行您在异常行中放入的所有内容,并继续执行代码。来自docs

如果在执行try子句期间发生异常,则其余 该子句被跳过。然后,如果其类型与名为的异常匹配 在except关键字之后,将执行except子句,然后 在try语句之后继续执行。

这就是为什么您永远不会离开while循环的原因。这是循环的快速解决方案:

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: '{}'" .format(err))
        break

如果找不到csv,进一步的调整取决于您要执行的操作。

相关问题