Python_try_except_finally
2014-10-19 20:26
489 查看
[python]
view plaincopyprint?
x = 'abc'
def fetcher(obj, index):
return obj[index]
fetcher(x, 4)
输出:
[plain]
view plaincopyprint?
File "test.py", line 6, in <module>
fetcher(x, 4)
File "test.py", line 4, in fetcher
return obj[index]
IndexError: string index out of range
第一: try不仅捕获异常,而且会恢复执行
[python]
view plaincopyprint?
def catcher():
try:
fetcher(x, 4)
except:
print "got exception"
print "continuing"
[plain]
view plaincopyprint?
got exception
continuing
第二:无论try是否发生异常,finally总会执行
[python]
view plaincopyprint?
def catcher():
try:
fetcher(x, 4)
finally:
print 'after fecth'
[plain]
view plaincopyprint?
after fecth
Traceback (most recent call last):
File "test.py", line 55, in <module>
catcher()
File "test.py", line 12, in catcher
fetcher(x, 4)
File "test.py", line 4, in fetcher
return obj[index]
IndexError: string index out of range
第三:try无异常,才会执行else
[python]
view plaincopyprint?
def catcher():
try:
fetcher(x, 4)
except:
print "got exception"
else:
print "not exception"
[plain]
view plaincopyprint?
got exception
[python]
view plaincopyprint?
def catcher():
try:
fetcher(x, 2)
except:
print "got exception"
else:
print "not exception"
[plain]
view plaincopyprint?
not exception
第四:利用raise传递异常
[python]
view plaincopyprint?
def catcher():
try:
fetcher(x, 4)
except:
print "got exception"
raise
[plain]
view plaincopyprint?
got exception
Traceback (most recent call last):
File "test.py", line 37, in <module>
catcher()
File "test.py", line 22, in catcher
fetcher(x, 4)
File "test.py", line 4, in fetcher
return obj[index]
IndexError: string index out of range
异常在程序代码中消失,可以通过raise重新引发该异常。
第五:except(name1, name2)
[python]
view plaincopyprint?
def catcher():
try:
fetcher(x, 4)
except(TypeError, IndexError):
print "got exception"
else:
print "not exception"
[python]
view plaincopyprint?
def catcher():
try:
fetcher(x, 4)
except:
print "got exception"
view plaincopyprint?
x = 'abc'
def fetcher(obj, index):
return obj[index]
fetcher(x, 4)
x = 'abc' def fetcher(obj, index): return obj[index] fetcher(x, 4)
输出:
[plain]
view plaincopyprint?
File "test.py", line 6, in <module>
fetcher(x, 4)
File "test.py", line 4, in fetcher
return obj[index]
IndexError: string index out of range
File "test.py", line 6, in <module> fetcher(x, 4) File "test.py", line 4, in fetcher return obj[index] IndexError: string index out of range
第一: try不仅捕获异常,而且会恢复执行
[python]
view plaincopyprint?
def catcher():
try:
fetcher(x, 4)
except:
print "got exception"
print "continuing"
def catcher(): try: fetcher(x, 4) except: print "got exception" print "continuing"输出:
[plain]
view plaincopyprint?
got exception
continuing
got exception continuing
第二:无论try是否发生异常,finally总会执行
[python]
view plaincopyprint?
def catcher():
try:
fetcher(x, 4)
finally:
print 'after fecth'
def catcher(): try: fetcher(x, 4) finally: print 'after fecth'输出:
[plain]
view plaincopyprint?
after fecth
Traceback (most recent call last):
File "test.py", line 55, in <module>
catcher()
File "test.py", line 12, in catcher
fetcher(x, 4)
File "test.py", line 4, in fetcher
return obj[index]
IndexError: string index out of range
after fecth Traceback (most recent call last): File "test.py", line 55, in <module> catcher() File "test.py", line 12, in catcher fetcher(x, 4) File "test.py", line 4, in fetcher return obj[index] IndexError: string index out of range
第三:try无异常,才会执行else
[python]
view plaincopyprint?
def catcher():
try:
fetcher(x, 4)
except:
print "got exception"
else:
print "not exception"
def catcher(): try: fetcher(x, 4) except: print "got exception" else: print "not exception"输出:
[plain]
view plaincopyprint?
got exception
got exception
[python]
view plaincopyprint?
def catcher():
try:
fetcher(x, 2)
except:
print "got exception"
else:
print "not exception"
def catcher(): try: fetcher(x, 2) except: print "got exception" else: print "not exception"输出:
[plain]
view plaincopyprint?
not exception
not exceptionelse作用:没有else语句,当执行完try语句后,无法知道是没有发生异常,还是发生了异常并被处理过了。通过else可以清楚的区分开。
第四:利用raise传递异常
[python]
view plaincopyprint?
def catcher():
try:
fetcher(x, 4)
except:
print "got exception"
raise
def catcher(): try: fetcher(x, 4) except: print "got exception" raise输出:
[plain]
view plaincopyprint?
got exception
Traceback (most recent call last):
File "test.py", line 37, in <module>
catcher()
File "test.py", line 22, in catcher
fetcher(x, 4)
File "test.py", line 4, in fetcher
return obj[index]
IndexError: string index out of range
got exception Traceback (most recent call last): File "test.py", line 37, in <module> catcher() File "test.py", line 22, in catcher fetcher(x, 4) File "test.py", line 4, in fetcher return obj[index] IndexError: string index out of rangeraise语句不包括异常名称或额外资料时,会重新引发当前异常。如果希望捕获处理一个异常,而又不希望
异常在程序代码中消失,可以通过raise重新引发该异常。
第五:except(name1, name2)
[python]
view plaincopyprint?
def catcher():
try:
fetcher(x, 4)
except(TypeError, IndexError):
print "got exception"
else:
print "not exception"
def catcher(): try: fetcher(x, 4) except(TypeError, IndexError): print "got exception" else: print "not exception"捕获列表列出的异常,进行处理。若except后无任何参数,则捕获所有异常。
[python]
view plaincopyprint?
def catcher():
try:
fetcher(x, 4)
except:
print "got exception"
def catcher(): try: fetcher(x, 4) except: print "got exception"
相关文章推荐
- python中的try/except/else/finally语句--自我小结
- Python中的try...except...finally
- Python try/except/finally应用
- python中 try、except、finally 的执行顺序
- A Byte of Python 笔记(11)异常:try..except、try..finally
- python try except else finally 执行顺序详细分析
- Python中异常处理简单示例(try,except,finally)
- Python中关于try/except/finally的基础用法
- python异常处理try,except,else,finally,raise
- python try/except/finally
- python try except finally raise
- 【python】错误 调试 测试try except finally raise
- 【python】错误 调试 测试try except finally raise
- python 异常处理-try-except-else-finally
- python中的try/except/else/finally语句
- python try except else finally 执行顺序详细分析
- python 学习笔记——try,except,else,finally
- python try..except..finally 实验【细节决定成败】
- python中的异常处理:try...except...else...finally语句
- python 2.4 不支持 try-except-finally