• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Python标准异常开发经验总结

python 搞代码 4年前 (2022-01-09) 14次浏览 已收录 0个评论

在我们编写脚本或者进行软件开发过程中总会遇见很多的异常和错误,而python里面有两个非常重要的功能,能够很好的处理异常和任何意外错误,这两个功能就是异常处理和断言。

异常示例:

1、FileNotFoundError尝试打开文d:\\openstack.txt文件由于文件不存在就会报出FIleNotFoundError:f=open("d:\\openstack.txt")Traceback (most recent call last):  File "<pyshell#0>", line 1, in <module>    f=open("d:\\openstack.txt")FileNotFoundError: [Errno 2] No such file or directory: 'd:\\openstack.txt' 2、ZeroDivisionError除法运算或取模运算中,除数为零的情况 >>> 5/0Traceback (most recent call last):  File "<pyshell#1>", line 1, in <module>    5/0ZeroDivisionError: division by zero3、ImportError一般是在导入模块时,由于模块不存在等原因导致报错>>> import  wwwwTraceback (most recent call last):  File "<pyshell#2>", line 1, in <module>    import  wwwwImportError: No module named 'wwww'4、ValueError 一般是由于传入参数的数据类型错误引起报错。 >>> a="sterc">>> int(a)Traceback (most recent call last):  File "<pyshell#4>", line 1, in <module>    int(a)ValueError: invalid literal for int() with base 10: 'sterc'>>>

二、异常处理

python中包含两种异常处理语句,可以使用try…except…finally或者try…except..else语句来处理异常,接下来简单的介绍两种语句的语法以及两者的区别:

try语句原理:

首先,执行try子句(在关键字try和关键字except之间的语句)

如果没有异常发生,忽略except子句,try子句执行后结束。

如果在执行try子句的过程中发生了异常,那么try子句余下的部分将被忽略。如果异常的类型和 except 之后的名称相符,那么对应的except子句将被执行。最后执行 try 语句之后的代码。

如果一个异常没有与任何的except匹配,那么这个异常将会传递给上层的try中。

一个 try 语句可能包含多个except子句,分别来处理不同的特定的异常。最多只有一个分支会被执行。

处理程序将只针对对应的try子句中的异常进行处理,而不是其他的 try 的处理程序中的异常。

一个except子句可以同时处理多个异常,这些异常将被放在一个括号里成为一个元组。

try…except..else语法:

try:   You do your operations here   ......................except ExceptionI:   If there is ExceptionI, then execute this block.except ExceptionII:   If there is ExceptionII, then execute this block.   ......................else:   If there is no exception then execute this block.     单个 try 语句可以有多个except语句。 当 try 块包含可能抛出不同类型的异常声明这是有用的也可以提供一个通用的 except 子句来处理异常(不提倡)except子句后,可以包括 else 子句。 如果代码在try:块不引发异常则代码在 else 块执行else是可以正常运行部存在异常的代码,这不需要 try: 块的保护  try:   fh = open("testfile", "w+")   fh.write("This is my test file for exception handling!!")except IOError:   print ("Error: can\'t find file or read data")else:   print ("Written content in the file successfully")   fh.close()     正常执行后就会生成testfile文件里面的内容为:This is my test file for exception handling!  而控制台输出结果如下:  Written content in the file successfully

用except子句处理多个异常

刚才我们的示例是处理了一个类型的异常,而except可以处理多个类型的异常例如

except(ValueError,ImportError,RuntimeError)这是可以将多个类型的标准错误写成一个元组,但是做个类型容易造成我们队类型的报错分析难度大。

try:   fh = open("testfile", "r")   fh.write("This is my test file for exception handling!!")except (ValueError,ImportError,RuntimeError):   print ("Error: can\'t find file or read data")else:   print ("Written content in the file successfully")   fh.close()  结果:  Error: can't find file or read data  #这里没有报出是那个类型的标准错误。

try-finally子句:

使用 try: 块. finally 块是必须执行,而不管 try 块是否引发异常或没有。try-finally 语句的语法是这样的。

try:   You do your operations here;   ......................   Due to any exception, this may be skipped.
except ExceptionI:   If there is ExceptionI, then execute this block.except ExceptionII:   If there is ExceptionII, then execute this block.

finally:

   This would always be executed.

try…except…finally无论try块能否正常的执行,finally是一定会执行的模块。

try:   fh = open("testfile", "r")   fh.write("This is my test file for exception handling!!")except (ValueError,ImportError,RuntimeError):   print ("Error: can\'t find file or read data")finally:   print ("Written content in the file successfully")   fh.close() #关闭文件  结果:文件中没有写入内容,  控制台输出以下内容:Error: can't find file or read dataWritten content in the file successfully

引发异常

可以通过使用 raise 语句触发几个方面的异常。对于 raise 语句的一般语法如下。

语法

raise [Exception [, args [, traceback]]]

这里,Exception 是异常的类型(例如,NameError)argument 为异常的参数值。该参数是可选的;如果没有提供,异常的参数是None。

最后一个参数 traceback,也可选的(实践中很少使用),并且如果存在的话,是用于异常的回溯对象。

示例

异常可以是一个字符串,一个类或一个对象。大多数Python的异常核心是触发类异常,使用类的一个实例参数的异常。定义新的异常是很容易的,可以按如下做法 –

def functionName( level ):    if level <1:        raise Exception(level)        # The code below to this would not be executed        # if we raise the exception    return level

注意:为了捕捉异常,一个“except”语句必须是指出抛出类对象异常或简单的字符串异常。例如,捕获异常上面,我们必须编写 except 子句如下 –

try:   Business Logic here...except Exception as e:   Exception handling here using e.args...else:   Rest of the code here...

下面的例子说明如何使用触发异常:

#!/usr/bin/python3def functionName( level ):    if level <1:        raise Exception(level)        # The code below to this would not be executed        # if we raise the exception    return level try:    l=functionName(-10)    print ("level=",l)except Exception as e: <strong style="color:transparent">本文来源gao@daima#com搞(%代@#码网@</strong>   print ("error in level argument",e.args[0])

这将产生以下结果

error in level argument -10

用户定义的异常

Python中,还可以通过内置的异常标准的派生类来创建自己的异常。

这里是关于 RuntimeError 的一个例子。这里一个类被创建,它是 RuntimeError 的子类。当需要时,一个异常可以捕获用来显示更具体的信息,这非常有用。

在try块,用户定义的异常将引发,并夹在 except 块中。 变量e是用来创建网络错误 Networkerror 类的实例。

class Networkerror(RuntimeError):   def __init__(self, arg):      self.args = arg

所以上面的类定义后,可以引发异常如下 –

try:   raise Networkerror("Bad hostname")except Networkerror,e:   print e.args

以上就是Python标准异常开发经验总结的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python标准异常开发经验总结
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址