Please take a look at
class ShortInputException(Exception): #QUESTION: Why do I have to inherit from the "Exception" class? def __init__(self, length, atleast): Exception.__init__(self) #QUESTION: Why do I have to add this? self.length = length self.atleast = atleast try: s = raw_input('Enter something --> ') if len(s) < 3: raise ShortInputException(len(s), 3) except EOFError, object_EOF: #QUESTION: How do I find the data variables of object_EOF? print '\nWhy did you do an EOF on me?' except ShortInputException, object_SIE: print 'ShortInputException: The input was of length %d, \ was expecting at least %d' % (object_SIE.length, object_SIE.atleast)
Ok, so I have three questions, both shown in the above code. I have the above example from "Byte of Python":
1) Why do I have to inherit from the exception-class in order to create my own exceptions? I mean, what parameters are inherited that I cannot create myself?
2) Why do I have to call the constructor of the Exception class?
3) I have included the saving of the error object in object_EOF, but how do I find out what data/methods object_EOF contains?