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

# -*- 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 )





你可能感兴趣的:(Python学习)