+ -
当前位置:首页 → 问答吧 → [问题]Linux下的编程分那些啊?c,perl,python?

[问题]Linux下的编程分那些啊?c,perl,python?

时间:2011-10-21

来源:互联网

代码:
#!/usr/bin/python
# 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 '\nWhy did you do an EOF on me?'
except 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.'

其中
except ShortInputException, x

这句我不懂:为什么x变成了ShortInputException类的对象了呢?

作者: Zhang-Xiao-Jun   发布时间: 2011-10-21

except ShortInputException, x
意思就是,捕捉ShortInputException异常,保存到x变量。相当于:
except ShortInputException as x

貌似是吧,我只用过后一种格式。

作者: cuihao   发布时间: 2011-10-21

代码:
#!/usr/bin/python
# 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 '\nWhy did you do an EOF on me?'
except 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.'

其中
except ShortInputException, x

这句我不懂:为什么x变成了ShortInputException类的对象了呢?

作者: Zhang-Xiao-Jun   发布时间: 2011-10-21

except ShortInputException, x
意思就是,捕捉ShortInputException异常,保存到x变量。相当于:
except ShortInputException as x

貌似是吧,我只用过后一种格式。

作者: cuihao   发布时间: 2011-10-21

热门下载

更多