收藏文章 楼主

处理异常

版块:python3 脚本程序   类型:精华   作者:小绿叶技术博客   查看:3799   回复:0   获赞:5   时间:2018-12-07 11:07:08

try:

    x,y = 10,20

    a = x/y

    print(a)

except:

    print("除错误和A没有定义");

#==================================================

class shortInputException(Exception):

    def __init__(self, length, atleast):

        Exception.__init__(self)

        self.length = length

        self,atleast = atleast

try:

    s = input('请输入三个以上字符:')

    if len(s) < 3:

        raise shortInputException(len(s), 3)

except shortInputException as x:

    print ('shortInputException: 长度是 %d, 至少应该是 %d' % (x.length, x.atleast))

else:

    print ('没有发生异常')

===================================================

a = 1

b = 2

c = 8

try :

    assert a < b

    d = a + c

    print (d)

except AssertionError:            #断言语句失败

    print ('a>b,语句表达式数据类型错误')

except TypeErrot as e:

    print ('语句表达式数据类型错误', e)

======================================================


提供企业建站服务,免费网防系统,提交信息登录 http://yundun.ddoss.cn 邮箱: proposal@ddoss.cn 
回复列表
默认   热门   正序   倒序

回复:处理异常

头像

用户名:

粉丝数:

签名:

资料 关注 好友 消息