| 异常 | 描述 |
| NameError | 尝试访问一个没有申明的变量 |
| ZeroDivisionError | 除数为0 |
| SyntaxError | 语法错误 |
| IndexError | 索引超出序列范围 |
| KeyError | 请求一个不存在的字典关键字 |
| IOError | 输入输出错误(比如你要读的文件不存在) |
| AttributeError | 尝试访问未知的对象属性 |
| ValueError | 传给函数的参数类型不正确,比如给int()函数传入字符 |
try: try_suite except Exception1,Exception2,...,Argument: exception_suite ...... #other exception block else: no_exceptions_detected_suite finally: always_execute_suite
try: try_suite except: exception block
try: try_suite except Exception: exception block
>>> try: ... res = 2/0 ... except ZeroDivisionError: ... print "Error:Divisor must not be zero!" ... Error:Divisor must not be zero!
try:
floatnum = float(raw_input("Please input a float:"))
intnum = int(floatnum)
print 100/intnum
except ZeroDivisionError:
print "Error:you must input a float num which is large or equal then 1!"
except ValueError:
print "Error:you must input a float num!"
[root@Cherish tmp]# python test.py
Please input a float:fjia
Error:you must input a float num!
[root@Cherish tmp]# python test.py
Please input a float:0.9999
Error:you must input a float num which is large or equal then 1!
[root@Cherish tmp]# python test.py
Please input a float:25.091
4
>>> try:
... 1/0
... except ZeroDivisionError,reason:
... pass
...
>>> type(reason)
<type 'exceptions.ZeroDivisionError'>
>>> print reason
integer division or modulo by zero
>>> reason
ZeroDivisionError('integer division or modulo by zero',)
>>> reason.__class__
<type 'exceptions.ZeroDivisionError'>
>>> reason.__class__.__doc__
'Second argument to a division or modulo operation was zero.'
>>> reason.__class__.__name__
'ZeroDivisionError'
>>> 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()
>>> 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()
>>> 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!
>>> 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'>
>>> with open('/root/test.py') as f:
... for line in f:
... print line
>>> 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')
>>> 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> #跟踪记录对象
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有