Using Try/Except Blocks for Error Handling

Python 异常处理

  • 程序执行中出现的错误提示,对于我们开发人员来说是很方便的,但对于用户来说是不友好的的,因此需要我们定制这些提示信息。
  • try的工作原理是,当开始一个try语句后,python就在当前程序的上下文中作标记,这样当异常出现时就可以回到这里,try子句先执行,接下来会发生什么依赖于执行时是否出现异常。

try and except

try:
    f = open("name1.csv") # 该文件就不存在
except Exception:
    print("sorry. this file does not exist")
sorry. this file does not exist

Exception类错误可以捕捉所有类型的error,比如我们增加一个 未定义的error var = bad_var:

try:
    f = open("name.csv") # 并没有这个文件  
    var = bad_var
except Exception:
    print("sorry. this file does not exist")
sorry. this file does not exist

可以看到,仍然执行了 except Exception下的语句。但实际来说,"name.csv"文件是存在的,引起错误的原因是’bad_var’没有定义,所以,我们需要分别捕捉不同类型的错误,并分别做针对性的处理。

try:
    f = open("name.csv") # 该文件存在
    var = bad_var
except FileNotFoundError:
    print("sorry. this file does not exist")
except Exception as e:
    print(e)

name 'bad_var' is not defined

需要注意的是我们将捕捉特定错误的except语句放到前面,而把可以捕捉任意错误的exception类的语句放到最后,以确保可以捕捉到这个类型

else and finally

  • 当try逻辑块中的代码执行后产生了问题,依次轮询except语句,执行符合要求的except语句中的代码,然后跳过else,执行finally语句
  • finally中执行的语句一般为关闭或者任务的后续事宜,比如关闭数据库等操作。目的是即使出现问题,也要保证系统的安全性
try:
    f = open("name1.csv") # 该文件存在
    # print(f.readline()) # 如果把这两句话放到这里,一起捕捉错误,这样就无法确保错误到底是由哪条语句产生的,故还是放到else语句中
    # f.close()
except FileNotFoundError:
    print("sorry. this file does not exist")
except Exception as e:
    print(e)
else:
    print(f.readline()) # 当前面的except语句确定没有执行,即前面try逻辑块中的语句执行没有产生错误,才会执行else里面的内容
    f.close()
finally:
    print("whaterver happened before, this statement will be executed surely") 
sorry. this file does not exist
whaterver happened before, this statement will be executed surely
  • 当try逻辑块中的代码执行后没有问题,则直接跳过except的所有语句,执行else语句,然后执行finally语句
try:
    f = open("name.csv") # 该文件存在
    # print(f.readline()) # 如果把这两句话放到这里,一起捕捉错误,这样就无法确保错误到底是由哪条语句产生的,故还是放到else语句中
    # f.close()
except FileNotFoundError:
    print("sorry. this file does not exist")
except Exception as e:
    print(e)
else:
    print(f.readline()) # 当前面的except语句确定没有执行,即前面try逻辑块中的语句执行没有产生错误,才会执行else里面的内容
    f.close()
finally:
    print("whaterver happened before, this statement will be executed surely") # 无论前面执行情况如何,该语句肯定执行
first_name,last_name,email

whaterver happened before, this statement will be executed surely
  • 当try逻辑块中的代码执行后产生了问题,依次轮询except语句,执行符合要求的except语句中的代码,如果except 的错误条件均不符合,则程序结束报错,不会执行后面的finally语句
  • 注意:如果存在上层try的话,异常将被递交到上层的try,或者到程序的最上层(这样将结束程序,并打印缺省的出错信息)。
try:
    f = open("name.csv") # 该文件存在
    var = bad_env
except FileNotFoundError:
    print("sorry. this file does not exist")
else:
    print(f.readline()) # 当前面的except语句确定没有执行,即前面try逻辑块中的语句执行没有产生错误,才会执行else里面的内容
    f.close()
finally:
    print("whaterver happened before, this statement will be executed surely") # 无论前面执行情况如何,该语句肯定执行
whaterver happened before, this statement will be executed surely



---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

<ipython-input-7-cc446b6489b7> in <module>
      1 try:
      2     f = open("name.csv") # 该文件存在
----> 3     var = bad_env
      4 except FileNotFoundError:
      5     print("sorry. this file does not exist")


NameError: name 'bad_env' is not defined
  • 手动触发异常

raise 后面的异常类型必须是标准的错误类型,且保证后面的except语句中包含对这个错误类型的处理语句。

try:
    f = open("name.csv") # 该文件存在
    if f.name == "name.csv":
        raise Exception
except FileNotFoundError:
    print("sorry. this file does not exist")
except Exception:
    print("这个异常是手动触发的")
else:
    print(f.readline()) # 当前面的except语句确定没有执行,即前面try逻辑块中的语句执行没有产生错误,才会执行else里面的内容
    f.close()
finally:
    print("whaterver happened before, this statement will be executed surely") # 无论前面执行情况如何,该语句肯定执行
这个异常是手动触发的
whaterver happened before, this statement will be executed surely

引用

本文主要参考下列视频内容,翻译并亲测代码后形成此文,感谢视频作者的无私奉献!

也参考了下面的内容作为了补充(包含更多错误处理的细节和所有的错误类型列表,感谢)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值