码迷,mamicode.com
首页 > 编程语言 > 详细

python's thirty day for me 异常处理

时间:2018-04-28 22:25:47      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:exce   如何   for   必须   value   span   while   一个   cep   

---恢复内容开始---

程序的异常:报错之后程序终止。

  异常处理搭配使用:

l = [创建老师,创建学校]
while True:
    try:
        for num,item in enumerate(l,1):
            print(num,item)
        index = int(input(num:))
        print(l[index - 1])
        break
    except ValueError:
        print(请输入一个数字:)
    except IndexError:
        print(请输入选择范围内的数字:)
    except Exception as e:
        print(e)

finally 语句 :必须要和try 搭配使用:

  finally 执行 try 中的代码,不管是否触发了错误,都会执行finally中的代码。

try:
    name
except NameError:
    print(name error)
else:
    print(sucess)
finally:
    print(finally)

try except:try中的代码遇到异常,就执行except中的代码。

try except else:try中的代码遇到异常,就执行except中的代码,没遇到异常就执行else中的代码。

try except else finally:try中的代码遇到异常,就执行except中的代码,没遇到异常就执行else中的代码,无                                论如何都执行finally中的代码。

try finally:不能处理异常了,但是无论是否发生异常,都会执行finally中的代码。

主动触发异常:raise

 

---恢复内容结束---

python's thirty day for me 异常处理

标签:exce   如何   for   必须   value   span   while   一个   cep   

原文地址:https://www.cnblogs.com/stfei/p/8965768.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!