python 处理异常问题,求解决

love-py love-py 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
class ShortInputException(Exception):
'''A user-defined exception class.'''
def __init__(self,length,atleast):
Exception.__init__(self)
self.length=length
self.atleast=atleast

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

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.')


问题:
Enter something -->
Traceback (most recent call last):
File "C:\Documents and Settings\Administrator\桌面\PYTHON\raising.py", line 15, in <module>
except ShortInputException(x):
NameError: name 'x' is not defined
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
love-py
脚本语言 37,738 成员 34,208 内容