Created
August 17, 2013 04:07
-
-
Save wzpan/6255208 to your computer and use it in GitHub Desktop.
Python - raise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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: | |
text = input('Enter something --> ') | |
if len(text) < 3: | |
raise ShortInputException(len(text), 3) | |
# Other work can continue as usual here | |
except EOFError: | |
print('Why did you do an EOF on me?') | |
except ShortInputException as ex: | |
print('ShortInputException: The input was {0} long, expected at least {1}'\ | |
.format(ex.length, ex.atleast)) | |
else: | |
print('No exception was raised.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment