自定义异常

# -- coding: utf-8 --
#自定义异常
class myException(Exception):
    pass
def division(x,y):
    if y == 1 :
        raise myException('y==1')  #假如当y==1时,抛出自定义异常
    return x/y

try:
    division(1,0)
except ZeroDivisionError as e:    
    print(e)

try:
    division(1,1)    #抛出异常
except myException as e:
    print(e)
division by zero
y==1
posted @ 2014-06-08 17:03  sxcww  阅读(179)  评论(0编辑  收藏  举报