绕过并打印出循环中错误的名称 - Python

Bypassing and printing out the name of an Error in a Loop - Python

虽然我知道绕过错误不是好的做法,但我试图找出我正在处理的数据集中的哪个数据集,我得到了一个错误。

我正在遍历所有数据集,每次出现错误时,我都会使用以下方法标记它:

    try:
                # block raising an exception
    except:
       pass     # doing nothing on exception

我设法通过在 except 块上打印他们的名字来标记出现错误的那些,但我也想知道是否有人知道也打印出来的方法我得到的错误(只是为了确保它与我预期的错误完全相同)

for j in list:
  for i in range(len(j)):
        try:
            run_path = j.iloc[i]
            name = run_path.processed_file
            name1 = name.split('/')
            name_final = name1[2]
            print(name_final)

            time, angle, couple, rpm, Fx, Fy, Fz, Mx, My , Mz , U , V, H = load_data(path_data + run_path.processed_file)
            dt = time[1] - time[0]

            minrose, maxrose, minwave, maxwave = minmaxrose_minmmaxH(Fx, angle, H)

            recap['Cxmin_rose'] = minrose
            recap['Cymmax_rose'] = maxrose
            recap['Cxmin_houle'] = minwave
            recap['Cxmax_houle'] = maxwave

            recap.to_excel(os.path.join(path_data,'recap_essai - JRS.xlsx'))  

            print('')
        except:
            run_path = j.iloc[i]
            name = run_path.processed_file
            name1 = name.split('/')
            name_final = name1[2]
            print('')
            print(name_final + ' ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR')
            print('')
            pass

在这种情况下,list 是“数据集块”的列表,i 是每个块内的数据集。

我的目标是在名称旁边打印我遇到的错误类型。

谢谢!!

打印错误类型,可以使用如下代码

try:
# Your code
except Exception as e:
    print(type(e).__name__)

示例:

try:
    1/0
except Exception as e:
    print(type(e).__name__)
# Output:
# ZeroDivisionError

另见How to print an exception in Python?