看见就烦的异常

在写代码的时候,经常会遇到异常。遇见异常并不是一件让人愉悦的事情。今天来一起详细了解异常。

1.异常的种类

以下是常用的异常

 1 AttributeError 试图访问一个对象没有的树形,比如foo.x,但是foo没有属性x
 2 IOError 输入/输出异常;基本上是无法打开文件
 3 ImportError 无法引入模块或包;基本上是路径问题或名称错误
 4 IndentationError 语法错误(的子类) ;代码没有正确对齐
 5 IndexError 下标索引超出序列边界,比如当x只有三个元素,却试图访问x[5]
 6 KeyError 试图访问字典里不存在的键
 7 KeyboardInterrupt Ctrl+C被按下
 8 NameError 使用一个还未被赋予对象的变量
 9 SyntaxError Python代码非法,代码不能编译(个人认为这是语法错误,写错了)
10 TypeError 传入对象类型与要求的不符合
11 UnboundLocalError 试图访问一个还未被设置的局部变量,基本上是由于另有一个同名的全局变量,
12 导致你以为正在访问它
13 ValueError 传入一个调用者不期望的值,即使值的类型是正确的
14 
15 常用异常
 1 ArithmeticError
 2 AssertionError
 3 AttributeError
 4 BaseException
 5 BufferError
 6 BytesWarning
 7 DeprecationWarning
 8 EnvironmentError
 9 EOFError
10 Exception
11 FloatingPointError
12 FutureWarning
13 GeneratorExit
14 ImportError
15 ImportWarning
16 IndentationError
17 IndexError
18 IOError
19 KeyboardInterrupt
20 KeyError
21 LookupError
22 MemoryError
23 NameError
24 NotImplementedError
25 OSError
26 OverflowError
27 PendingDeprecationWarning
28 ReferenceError
29 RuntimeError
30 RuntimeWarning
31 StandardError
32 StopIteration
33 SyntaxError
34 SyntaxWarning
35 SystemError
36 SystemExit
37 TabError
38 TypeError
39 UnboundLocalError
40 UnicodeDecodeError
41 UnicodeEncodeError
42 UnicodeError
43 UnicodeTranslateError
44 UnicodeWarning
45 UserWarning
46 ValueError
47 Warning
48 ZeroDivisionError
49 
50 更多异常
更多异常
1 dic = ["wupeiqi", 'alex']
2 try:
3     dic[10]
4 except IndexError, e:
5     print e
实例:IndexError
1 dic = {'k1':'v1'}
2 try:
3     dic['k20']
4 except KeyError, e:
5     print e
实例:KeyError

对于上述实例,异常类只能用来处理指定的异常情况,如果非指定异常则无法处理。

1
2
3
4
5
6
7
# 未捕获到异常,程序直接报错
 
s1  =  'hello'
try :
     int (s1)
except  IndexError,e:
     print  e

所以,写程序时需要考虑到try代码块中可能出现的任意异常,可以这样写:

1
2
3
4
5
6
7
8
9
s1  =  'hello'
try :
     int (s1)
except  IndexError,e:
     print  e
except  KeyError,e:
     print  e
except  ValueError,e:
     print  e

万能异常 在python的异常中,有一个万能异常:Exception,他可以捕获任意异常,即:

1
2
3
4
5
s1  =  'hello'
try :
     int (s1)
except  Exception,e:
     print  e

接下来你可能要问了,既然有这个万能异常,其他异常是不是就可以忽略了!

答:当然不是,对于特殊处理或提醒的异常需要先定义,最后定义Exception来确保程序正常运行。

1
2
3
4
5
6
7
8
9
s1  =  'hello'
try :
     int (s1)
except  KeyError,e:
     print  '键错误'
except  IndexError,e:
     print  '索引错误'
except  Exception, e:
     print  '错误'

2.异常其他结构

1
2
3
4
5
6
7
8
9
10
11
12
try :
     # 主代码块
     pass
except  KeyError,e:
     # 异常时,执行该块
     pass
else :
     # 主代码块执行完,执行该块
     pass
finally :
     # 无论异常与否,最终执行该块
     pass

3.主动触发异常

1
2
3
4
try :
     raise  Exception( '错误了。。。' )
except  Exception,e:
     print  e

4.自定义异常

1
2
3
4
5
6
7
8
9
10
11
12
class  WupeiqiException(Exception):
 
     def  __init__( self , msg):
         self .message  =  msg
 
     def  __str__( self ):
         return  self .message
 
try :
     raise  WupeiqiException( '我的异常' )
except  WupeiqiException,e:
     print  e

5.断言

1
2
3
4
5
# assert 条件
 
assert  1  = =  1
 
assert  1  = =  2

转载于:https://www.cnblogs.com/lixiaoliuer/p/6540500.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值