zoukankan      html  css  js  c++  java
  • 用户输入内容长度限制的异常 分类: python异常 2013-06-24 10:48 335人阅读 评论(0) 收藏

    class ShortInputException(Exception):
        '''A user-defined exception class'''
        def __init__(self,length,atleast):
            Exception.__init__(self)
            self.length=length
            self.atleast=atleast

    try:

        s=raw_input('Enter something-->')
        if len(s)<3:
            raise ShortInputException(len(s),3)

    except KeyboardInterrupt: #当用户取消输入时,引发此异常
        print "Cancel it "

    except EOFError:
        print ' Why did you do an EOF on me?'

    except ShortInputException,x:

    '''如果引发该异常ShortInputException实例赋给后面那个名字x;当用户输入内容长度<3时,引发此异常;建议此处使用except ShortInputException as x:如果引发该异常ShortInputException实例赋给后面那个名字x

    '''

        print 'ShortInputException:The input was of length %d,
    was expecting at least %d' %(x.length,x.atleast)

    else:
        print 'No exception was raised'

  • 相关阅读:
    HttpWatch 有火狐版本?
    JQgrid的最新API
    jqgrid
    JSON的学习网站
    array创建数组
    Numpy安装及测试
    SQLite3删除数据_7
    SQLite3修改数据_6
    SQLite3查询一条数据_5
    SQLite3查询所有数据_4
  • 原文地址:https://www.cnblogs.com/think1988/p/4628151.html
Copyright © 2011-2022 走看看