zoukankan      html  css  js  c++  java
  • 抛出异常

    from pip._vendor.distlib.compat import raw_input
    class ShortInputException(Exception):
        '''你定义的异常类。'''
        def __init__(self, length, atleast):
            Exception.__init__(self)
            self.length = length
            self.atleast = atleast
    
    try:
        s = raw_input('请输入 --> ')
    
        if len(s) < 3:
            # raise引发一个你定义的异常
            raise ShortInputException(len(s), 3)
    
    except EOFError:
        print ('/n你输入了一个结束标记EOF')
    except ShortInputException, x:#x这个变量被绑定到了错误的实例
        print('ShortInputException: 输入的长度是 %d,长度至少应是 %d'% (x.length, x.atleast))
    else:
        print ('没有异常发生.')
    

    运行结果:

        $ python raising.py
        请输入 -->
        你输入了一个结束标记EOF
    
        $ python raising.py
        请输入 --> --> ab
        ShortInputException: 输入的长度是 2, 长度至少应是 3
    
        $ python raising.py
        请输入 --> abc
        没有异常发生.
  • 相关阅读:
    学习进度
    毕设进度
    学习进度
    毕设进度
    学习进度
    学习进度
    第一周冲刺评论总结&&针对评论总结的改进
    第一阶段成果展示
    团队冲刺--Seven
    团队冲刺--six
  • 原文地址:https://www.cnblogs.com/loaderman/p/6560383.html
Copyright © 2011-2022 走看看