python 异常小结

1.Python异常类

异常描述
NameError尝试访问一个没有申明的变量
ZeroDivisionError除数为0
SyntaxError语法错误
IndexError索引超出序列范围
KeyError请求一个不存在的字典关键字
IOError输入输出错误(比如你要读的文件不存在)
AttributeError尝试访问未知的对象属性
ValueError传给函数的参数类型不正确,比如给int()函数传入字符串形

所有的异常,这个类是BaseException。

BaseException

    | - KeyboardInterrupt

    | - SystemExit

    | - Exception

2.捕获异常

    Python完整的捕获异常的语句有点像:

代码如下:

try:
    try_suite
except Exception1,Exception2,...,Argument:
    exception_suite
......   #other exception block
else:
    no_exceptions_detected_suite
finally:
    always_execute_suite

2.1.try...except...语句

try:
    try_suite
except:
    exception block

2.2try ... except...else语句

>>> import syslog
>>> try:
...     f = open("/root/test.py")
... except IOError,e:
...     syslog.syslog(syslog.LOG_ERR,"%s"%e)
... else:
...     syslog.syslog(syslog.LOG_INFO,"no exception caught\n")
...
>>> f.close()

2.3 finally子句

>>> import syslog
>>> try:
...     f = open("/root/test.py")
... except IOError,e:
...     syslog.syslog(syslog.LOG_ERR,"%s"%e)
... else:
...     syslog.syslog(syslog.LOG_INFO,"no exception caught\n")
... finally:
>>>     f.close()

3.两个特殊的处理异常的简便方法

3.1断言(assert)
    什么是断言,先看语法:

assert expression[,reason]
其中assert是断言的关键字。执行该语句的时候,先判断表达式expression,如果表达式为真,则什么都不做;

如果表达式不为真,则抛出异常。reason跟我们之前谈到的异常类的实例一样。

>>> assert len('love') == len('like')
>>> assert 1==1
>>> assert 1==2,"1 is not equal 2!"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError: 1 is not equal 2!

我们可以看到,如果assert后面的表达式为真,则什么都不做,如果不为真,

就会抛出AssertionErro异常,而且我们传进去的字符串会作为异常类的实例的具体信息存在。

其实,assert异常也可以被try块捕获:

>>> try:
...     assert 1 == 2 , "1 is not equal 2!"
... except AssertionError,reason:
...     print "%s:%s"%(reason.__class__.__name__,reason)
...
AssertionError:1 is not equal 2!
>>> type(reason)
<type 'exceptions.AssertionError'>

3.2.上下文管理(with语句)
   如果你使用try,except,finally代码仅仅是为了保证共享资源(如文件,数据)的唯一分配,并在任务结束后释放它,那么你就有福了!这个with语句可以让你从try,except,finally中解放出来!语法如下:

with context_expr [as var]:
    with_suite

是不是不明白?很正常,举个例子来!


>>> with open('/root/test.py') as f:
...     for line in f:
...         print line

上面这几行代码干了什么?
    (1)打开文件/root/test.py
    (2)将文件对象赋值给  f
    (3)将文件所有行输出
     (4)无论代码中是否出现异常,Python都会为我们关闭这个文件,我们不需要关心这些细节。
    这下,是不是明白了,使用with语句来使用这些共享资源,我们不用担心会因为某种原因而没有释放他。

但并不是所有的对象都可以使用with语句,只有支持上下文管理协议(context management protocol)的对象才可以,那哪些对象支持该协议呢?

如下表:
file

decimal.Context
thread.LockType
threading.Lock
threading.RLock
threading.Condition
threading.Semaphore
threading.BoundedSemaphore

    至于什么是上下文管理协议,如果你不只关心怎么用with,以及哪些对象可以使用with,那么我们就不比太关心这个问题:)

4.抛出异常(raise)

    如果我们想要在自己编写的程序中主动抛出异常,该怎么办呢?raise语句可以帮助我们达到目的。其基本语法如下:

raise [SomeException [, args [,traceback]]

第一个参数,SomeException必须是一个异常类,或异常类的实例
    第二个参数是传递给SomeException的参数,必须是一个元组。这个参数用来传递关于这个异常的有用信息。
    第三个参数traceback很少用,主要是用来提供一个跟中记录对象(traceback)
    下面我们就来举几个例子。

>>> raise NameError
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError
>>> raise NameError()  #异常类的实例
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError
>>> raise NameError,("There is a name error","in test.py")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
>>> raise NameError("There is a name error","in test.py")  #注意跟上面一个例子的区别
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: ('There is a name error', 'in test.py')
>>> raise NameError,NameError("There is a name error","in test.py")  #注意跟上面一个例子的区别
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: ('There is a name error', 'in test.py')

其实,我们最常用的还是,只传入第一个参数用来指出异常类型,最多再传入一个元组,用来给出说明信息。如上面第三个例子。

5.异常和sys模块

    另一种获取异常信息的途径是通过sys模块中的exc_info()函数。该函数回返回一个三元组:(异常类,异常类的实例,跟中记录对象)


>>> try:
...     1/0
... except:
...     import sys
...     tuple = sys.exc_info()
...
>>> print tuple
(<type 'exceptions.ZeroDivisionError'>, ZeroDivisionError('integer division or modulo by zero',), <traceback object at 0x7f538a318b48>)
>>> for i in tuple:
...     print i
...
<type 'exceptions.ZeroDivisionError'> #异常类    
integer division or modulo by zero #异常类的实例
<traceback object at 0x7f538a318b48> #跟踪记录对象



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值