python异常处理机制
if __name__ == '__main__':
# excel2df()
try:
i = 1
if i == 1:
try:
a = 1 / 0;
except BaseException as e: # 异常基类
except_str = '自定义的异常信息1-->' + str(e)
raise Exception(except_str)
elif i == 2:
raise Exception('自定义异常信息2')
else:
print('没有异常,执行try代码')
except BaseException as e:
print('有异常,打印异常信息:', e)
else:
print('没有异常,执行else代码')
finally:
print('不管是否有异常,执行finally代码')