raising.py

来自「python简明教程源代码」· Python 代码 · 共 23 行

PY
23
字号
# -*- coding: cp936 -*-
# Filename: raising.py

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)
    # Other work can continue as usual here
except EOFError:
    print '\n Why did you do an EOF on me?'
except ShortInputException, x:
    print 'ShortInputException: The input was of length %d, \
was excepting at least %d' % (x.length, x.atleast)
else:
    print 'No exception was raised.'

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?