全部博文(64)
分类: Python/Ruby
2009-09-22 10:36:03
#!/usr/bin/python
# Filename: try_except.pyimport systry: s = raw_input('Enter something --> ')except EOFError: print '\nWhy did you do an EOF on me?' sys.exit() # exit the programexcept: print '\nSome error/exception occurred.' # here, we are not exiting the programprint 'Done'
#!/usr/bin/python
# Filename: raising.pyclass ShortInputException(Exception): '''A user-defined exception class.''' def __init__(self, length, atleast):
Exception.__init__(self)
self.length = length
self.atleast = atleasttry:
s = raw_input('Enter something --> ') if len(s) < 3:
raise ShortInputException(len(s), 3) # Other work can continue as usual hereexcept 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.