python中的异常处理 try..except
来源:互联网 发布:下载word2010软件 编辑:程序博客网 时间:2024/05/17 22:21
一、try…except
有时候我们写程序的时候,会出现一些错误或异常,导致程序终止。例如,做除法时,除数为0,会引起一个ZeroDivisionError
例子:
a
=
10
b
=
0
c
=
a
/
b
print
"done"
运行结果:
Traceback (most recent call last):
File “C:/Users/lirong/PycharmProjects/untitled/openfile.py”, line 3, in <module>
c=a/b
ZeroDivisionError: integer division or modulo by zero
我们发现程序因为ZeroDivisionError而中断了,语句print “done” 没有运行。为了处理异常,我们使用try…except,更改代码:
a
=
10
b
=
0
try
:
c
=
a
/
b
print
c
except
ZeroDivisionError,e:
print
e.message
print
"done"
运行结果:
integer division or modulo by zero
done
这样程序就不会因为异常而中断,从而print “done”语句正常执行。
我们把可能发生错误的语句放在try模块里,用except来处理异常。except可以处理一个专门的异常,也可以处理一组圆括号中的异常,如果except后没有指定异常,则默认处理所有的异常。每一个try,都必须至少有一个except
处理一组异常可以这样写(其中e代表异常的实例):
try
:
pass
except
(IOError ,ZeroDivisionError),e:
print
e
try ….except…else 语句,当没有异常发生时,else中的语句将会被执行。
例子:
a
=
10
b
=
0
try
:
c
=
b
/
a
print
c
except
(IOError ,ZeroDivisionError),x:
print
x
else
:
print
"no error"
print
"done"
运行结果:
0
no error
done
二、raise 引发一个异常
例子:如果输入的数据不是整数,则引发一个ValueError
inputValue
=
input
(
"please input a int data :"
)
if
type
(inputValue)!
=
type
(
1
):
raise
ValueError
else
:
print
inputValue
假设输入1.2,运行结果为:
please input a int data :1.2
Traceback (most recent call last):
File “C:/Users/lirong/PycharmProjects/untitled/openfile.py”, line 3, in <module>
raise ValueError
ValueError
如果输入1,运行结果为:
please input a int data :1
1
三、try …finally
无论异常是否发生,在程序结束前,finally中的语句都会被执行。
a
=
10
b
=
0
try
:
print
a
/
b
finally
:
print
"always excute"
运行结果:
Traceback (most recent call last):
always excute
File “C:/Users/lirong/PycharmProjects/untitled/openfile.py”, line 4, in <module>
print a/b
ZeroDivisionError: integer division or modulo by zero
虽然有异常发生,但在程序终止前,finally中的语句也能正常执行。。
finally语句也可以和except语句一起使用。
a
=
10
b
=
0
try
:
print
a
/
b
except
:
print
"error"
finally
:
print
"always excute"
运行结果:
error
always excute
四、自定义一个异常类
自定义一个MyException类,继承Exception。
class
MyException(Exception):
def
__init__(
self
,message):
Exception.__init__(
self
)
self
.message
=
message
如果输入的数字小于10,就引发一个MyException异常:
a
=
input
(
"please input a num:"
)
if
a<
10
:
try
:
raise
MyException(
"my excepition is raised "
)
except
MyException,e:
print
e.message
运行结果:
please input a num:1
my excepition is raised
五、python所有的标准异常类:
- python中的异常处理 try..except
- Python 异常处理 try/except
- python异常处理try...except
- python中的异常处理:try...except...else...finally语句
- python中的异常捕获处理机制(try...except...等语句)
- python中的异常处理try/except/finally/raise
- Python 异常处理 Python 基础教程 try..except
- (Python)异常处理try...except、raise
- (Python)异常处理try...except、raise
- Python异常处理try...except、raise
- Python---异常处理try...except、raise
- (Python)异常处理try...except、raise
- Python异常处理try...except、raise
- python try...except....else...finally处理异常
- Python 异常处理 (try 和 except)
- try,except异常处理
- Python 异常 try except
- Python3:异常处理,try...except
- 温度计式的时间进度(前端样式)
- LintCode 469-等价二叉树
- mybatis框架之 log4j.properties 的配置
- 计算三个班同学的总成绩,并升序排列
- 十年过去了,各位 .net 兄弟还好吗
- python中的异常处理 try..except
- hadoop2.x之IO:序列化
- lintcode--x的平方根
- CSS中的选择器
- CSU 1598: 最长公共前缀(KMP)
- (UVA
- 51Nod 线段的重合
- c++string与数字的互转
- linux 磁盘存储相关命令