python try/except/finally
来源:互联网 发布:网店美工课程设计 编辑:程序博客网 时间:2024/06/10 00:36
稍微总结一下,否则总是忘。
[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"
0 0
- python try/except/finally
- python try/except/finally
- python try/except/finally
- python try/except/finally
- python try/except/finally
- python try/except/finally
- python try/except/finally
- python - try/except/else/finally
- python try except finally raise
- python try except finally raise
- Python中的try...except...finally
- python try except else finally
- python 2.4 不支持 try-except-finally
- python中的try/except/else/finally语句
- Python中try...except...finally的理解
- python try..except..finally 实验【细节决定成败】
- Python中try,except,else,finally用法
- python中的try/except/else/finally语句
- 机器学习实战:单变量线性回归的实现
- python如何保证输入键入数字
- python中input()与raw_input()的区别到底是啥?-----marsggbo原创作品为你解答
- python的try方法中的else和finally的区别
- linphone-去掉linphone的ContactsListFragment联系人界面
- python try/except/finally
- python安装图文教程---超详细。。。不过是转的,但有改动
- python的with语句,超级强大
- 解决win10 关键错误开始菜单和cortana无法工作 的问题(转-真的成功了)
- linux目录
- 这是对position讲解最通俗易懂的版本了。
- 数据建模讲解和案例分析
- 生日快乐~
- IDEA : Unable to save settings: Failed to save settings. Please restart IntelliJ IDEA解决办法