Python异常处理
异常处理是写出健壮程序的必备步骤。
1.1 基本语法
把可能抛出异常(出错)的语句放在try
的block
里,然后用except
去扑捉(预判)可能的异常类型,如果异常类型match,就执行except
模块。
try:
# write some code
# that might throw exception
except <ExceptionType>:
# Exception handler, alert the user
比如读取一个不存在的文件会引起IOError
,我们就可以提前加以处理。
try:
f = open('nofile.txt', 'r')
print f.read()
f.close()
except IOError:
print 'file not found'
file not found
执行流程是这样的:
- First statement between
try
andexcept
block are executed.- If no exception occurs then code under
except
clause will be skipped.- If file don’t exists then exception will be raised and the rest of the code in the
try
block will be skipped- When exceptions occurs, if the exception type matches exception name after
except
keyword, then the code in thatexcept
clause is executed.
1.2 扑获更多异常类型
try:
<body>
except <ExceptionType1>:
<handler1>
except <ExceptionTypeN>:
<handlerN>
except:
<handlerExcept>
else:
<process_else>
finally:
<process_finally>
-
except
类似于elif
,当try
出现异常时,挨个匹配except
里的异常类型,如果匹配,执行;若果没有匹配,执行不指定异常类型的except
。 -
else
只有在try
执行时没有异常的时候执行。 -
finally
不管try
模块是否有异常抛出,都执行。
1.3 例子
num1, num2 = 1,0
try:
result = num1 / num2
print("Result is", result)
except ZeroDivisionError:
print("Division by zero is error !!")
except:
print("Wrong input")
else:
print("No exceptions")
finally:
print("This will execute no matter what you input")
Division by zero is error !!
This will execute no matter what you input
2. 抛出异常
用raise
语句抛出自己的异常。raise ExceptionClass('Your argument')
def enterage(age):
if age < 0:
raise ValueError("Only positive integers are allowed")
if age % 2 == 0:
print("age is even")
try:
num = int(input("Enter your age: "))
enterage(num)
except ValueError:
print 'Only positive integers are allowd'
except:
print 'Something went wrong'
Enter your age: -3
Only positive integers are allowd
异常参考图,更多异常类型参见官方文档。
3. 操作异常
有时候我们希望能把异常对象传递给一个变量,也非常方便实现。
try:
# this code is expected to throw exception
except ExceptionType as ex:
# code to handle exception
try:
number = int(input("Enter a number: "))
print "The number entered is", number
except NameError as ex:
print "Exception:", ex
Enter a number: one
Exception: name 'one' is not defined