Python 练习代码 -- 异常,抛异常, 自定义异常

来源:互联网 发布:电脑怎么截图淘宝客服 编辑:程序博客网 时间:2024/05/19 18:13

# -*- mode: python; coding: utf-8 -*-import sys#自定义异常class MyException(Exception):    def __init__(self):        super(MyException,self).__init__()try:    file("hello.txt", "r")    raise MyException   #手动抛异常except IOError:    print("IOError: 文件不存在")except MyException:    print("MyException")except:    print(sys.exc_info()) #打印异常finally:    print("finally。。。")    #assert 检测 AssertionError   t = "hello"#assert( len(t)==6 )





0 0