首页 > 编程语言 > 详细

Python3 中异常的处理

时间:2017-03-22 16:37:58      阅读:176      评论:0      收藏:0      [点我收藏+]

1.普通异常的使用:

names = [‘wt‘,‘gxb‘]
data = {}
try:
    # names[3]
    # data[‘name‘]
    open(‘b.txt‘)
except IndexError as e:
    print(‘there is not exist this key‘,e)
except Exception as e:
    print(‘I can`t find this error!!!‘)
else:
    print(‘it`s ok!!‘)
finally:
    print(‘dajkdhkaj‘)

2.自己触发的异常:

class MyExpection(Exception):
    def __init__(self,msg):
        self.message = msg

try:
    raise MyExpection(‘触发了异常‘)
except MyExpection as e:
    print(e)

  

Python3 中异常的处理

原文:http://www.cnblogs.com/hjc4025/p/6600414.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!