Python 学习笔记 (3)—— python异常处理

       python通过使用try...except来处理异常,而通过raise来引发异常。

异常在 Python 中无处不在;实际上在标准 Python 库中的每个模块都使用了它们,并且 Python 自已会在许多不同的情况下引发它们。例如:


· 使用不存在的字典关键字 将引发 KeyError 异常。

· 搜索列表中不存在的值 将引发 ValueError 异常。

· 调用不存在的方法 将引发 AttributeError 异常。

· 引用不存在的变量 将引发 NameError 异常。

· 未强制转换就混用数据类型 将引发 TypeError 异常。


实际上异常指的是程序不能按照我们所想的那样执行,如下面的例子:

>>> Print 'hello world'
File "<stdin>", line 1
Print 'hello world'
^
SyntaxError: invalid syntax


由于把print 写成大写,导致了SyntaxError (语法错误)异常,通过提示我们也可以看到错误出现在line 1 即第一行,程序的排错就是根据这个提示再回去检查的。


当我们尝试读取用户输入的时候,按下ctrl+d 可以看到下面的效果:

>>> s = raw_input('Input something:')
Input something:Traceback (most recent call last):
File "<stdin>", line 1, in<module>
EOFError


引发了一个称为EOFError的错误,这个错误基本上意味着它发现一个不期望的“文件尾”


处理异常

可以使用try..except语句来处理异常。我们把要执行的语句放在try-块中,而把错误处理语句放在except-块中

例子:

#!/usr/bin/python
import sys
                                                                             
try:
        s = raw_input('Input something:')
except EOFError:
        print '\nWhy did you do an EOF on me?'
        sys.exit()
except:
        print '\nSome error/exception occurred.'
print 'Done'

执行结果

[root@node1 python]# python 2.py
Input something:
Why did you doan EOF on me?
[root@node1 python]# python 2.py
Input something:hello world
Done

       我们把所有可能引发错误的语句放在try块中,然后except从句/块中处理所有的错误和异常except从句可以专门处理单一的错误或异常,或者一组包括在圆括号内的错误/异常。如果没有给出错误或异常的名称,它会处理所有的错误和异常。对于每个try从句,至少都有一个相关联的except从句。

如果某个错误或异常没有被处理,默认的Python处理器就会被调用。它会终止程序的运行,并且打印一个消息,我们已经看到了这样的处理。

你还可以让try..catch块关联上一个else从句。当没有异常发生的时候,else从句将被执行

例子:

try:
    fsock = open("/notthere")
except IOError:
        print "The file does not exist, exiting gracefully"
print "This line will always print"

我们还可以得到异常对象,从而获取更多有个这个异常的信息。


引发异常

你可以使用raise语句 引发 异常。你还得指明错误/异常的名称和伴随异常 触发的 异常对象。你可以引发的错误或异常应该分别是一个Error或Exception类的直接或间接导出类

#!/usr/bin/python
class ShortInputException(Exception):
        '''A user-defined exception class.'''
        def __init__(self, length, atleast):
                Exception.__init__(self)
                self.length = length
                self.atleast = atleast
                                                                                         
try:
        s = raw_input('Enter something -->')
        if len(s) < 3:
                raise ShortInputException(len(s), 3)
except EOFError:
        print '\nWhy did you do and EOF on me?'
except ShortInputException, x:
        print 'ShortInputException: The input was of length %d, \
was excepting at least %d' % (x.length, x.atleast)
else:
        print 'No exception was raised.'


执行结果
[root@node1 python]# python 3.py
Enter something -->
Why did you do and EOF on me?
                                                                      
[root@node1 python]# python 3.py
Enter something -->ab
ShortInputException: The input was of length 2, was excepting at least 3
                                                                      
[root@node1 python]# python 3.py
Enter something -->abcd
No exception was raised.


     这里,我们创建了我们自己的异常类型,其实我们可以使用任何预定义的异常/错误。这个新的异常类型是ShortInputException类。它有两个域——length是给定输入的长度,atleast则是程序期望的最小长度。

在except从句中,我们提供了错误类和用来表示错误/异常对象的变量。这与函数调用中的形参和实参概念类似。在这个特别的except从句中,我们使用异常对象的length和atleast域来为用户打印一个恰当的消息。


try..finally

       假如你在读一个文件的时候,希望在无论异常发生与否的情况下都关闭文件,该怎么做呢?这可以使用finally块来完成。注意,在一个try块下,你可以同时使用except从句和finally块。如果你要同时使用它们的话,需要把一个嵌入另外一个。

#!/usr/bin/python
import time
try:
        f = file('test.txt')
        while True:
                line = f.readline()
                if len(line) == 0:
                        break
                time.sleep(2)
                print line,
finally:
        f.close()
        print 'Cleaning up...closed the file'

执行结果

[root@node1 python]# python 4.py
hello boy
hello girl
^CCleaning up...closed the file
Traceback (most recent call last):
  File "4.py", line 9, in <module>
    time.sleep(2)
KeyboardInterrupt


       在程序运行的时候,按Ctrl-c中断/取消程序。可以观察到KeyboardInterrupt异常被触发,程序退出。但是在程序退出之前,finally从句仍然被执行,把文件关闭



总结一下常见的错误类型

1、NameError:尝试访问一个未声明的变量任何可访问的变量必须在名称空间列出,访问变量需要由解释器进行搜索,如果请求的名字没有在任何名称空间里找到,那么将会生成一个 NameError异常

>>> defsay():
...     print'Hello'
...
>>> say()
Hello
>>> saya()
Traceback (most recent call last):
File"<stdin>", line 1, in<module>
NameError: name 'saya'isnotdefined


2、ZeroDivisionError:除数为零任何数值被零除都会导致一个ZeroDivisionError的异常

>>> 10/2
5
>>> 10/0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero


3、SyntaxError:Python解释器语法错误 SynaxError异常是唯一不是在运行时候发生的异常,它表示Python代码中有不正确的结构,因此无法正常执行。很明显,这个错误是在编译时候产生的,Python解释器无法将该脚本转化为Python字节代码。

>>> Print'hello python'
File"<stdin>", line 1
Print'hello python'
^
SyntaxError: invalid syntax


4、IndexError:请求的索引超出了序列范围

>>> l = [1,2,3,4]
>>> l[0]
1
>>> l[3]
4
>>> l[4]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range


5、KeyError:请求一个不存在的字典关键字字典中用key访问value,如果使用不存在的key,就是抛出KeyError异常

>>> d = {1:111,2:222,3:333}
>>> d[1]
111
>>> d[4]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 4


6、IOError:输入/输出错误如果尝试打开一个不存在或者无权限的文件等操作,就会引发操作系统I/O错误。这个错误可以多种

>>> f = open('/tmp/testpython')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: '/tmp/testpython'


7、AttributeError:尝试访问未知对象属性

#!/usr/bin/python
class Student():
        name = 'Pmghong'
        def fun1(self):
                name = self.name
                print 'My name is %s' % name
             
        def __fun2(self):
                print 'I am private method.'
s1 = Student()
s1.__fun2()
             
[root@node1 python]# python 15.py
Traceback (most recent call last):
  File "15.py", line 12, in <module>
    s1.__fun2()
AttributeError: Student instance has no attribute '__fun2'


8、ValueError:赋值异常

>>> int(123.123)
123
>>> int('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'a'


9、TypeError:类型错误,不同类型的两个变量相加就会出现这样的错误。

>>> a = 123
>>> b = '123'
>>> a + b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值