新手 看书疑问 问个问题...
#!/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 表示什么? 怎么 无缘无故就跑出来了...... 还有 类可以不用实例化 直接来用么??
# 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 表示什么? 怎么 无缘无故就跑出来了...... 还有 类可以不用实例化 直接来用么??
作者: fllintel 发布时间: 2011-05-20
x表示捕捉到的ShortInputException的实例
类是可以直接用的,具体的一句两句说不清楚,可以查看一下有关资料
类是可以直接用的,具体的一句两句说不清楚,可以查看一下有关资料
作者: cnff 发布时间: 2011-05-20