[EMAIL PROTECTED] wrote: > O/S: Windows XP Service Pack 2 > Python version: 2.4 > > Unable to understand how to build a class to handle an exception. > > Contents of sample01.py: > import exceptions > class SampleMain: > try: > def __init__(self): > print 'in SampleMain constructor' > > def Allowed(self): > print 'in allowed' > > def NotYetAllowed(self): > UCError = UnderConstructionError('not yet ready') > raise UCError > > except UnderConstructionError, e: > print e.msg > > class Error(exceptions.Exception): > def __init__(self): > print 'in base class constructor' > > class UnderConstructionError(Error): > def __init__(self, message): > print 'in UnderConstructionError constructor' > self.msg = message
! def __str__(self): ! return self.msg > > Copy/paste of interactive window: > PythonWin 2.4 (#60, Nov 30 2004, 09:34:21) [MSC v.1310 32 bit (Intel)] > on win32. > Portions Copyright 1994-2004 Mark Hammond ([EMAIL PROTECTED]) - > see 'Help/About PythonWin' for further copyright information. > >>> import sample01 > >>> x = sample01.SampleMain() > in SampleMain constructor > >>> x.NotYetAllowed() > in UnderConstructionError constructor > Traceback (most recent call last): > File "<interactive input>", line 1, in ? > File "C:\Python24\sample01.py", line 12, in NotYetAllowed > raise UCError > UnderConstructionError: <unprintable instance object> > >>> > > My questions are: > 1) What is causing the error described in the Traceback? The text <unprintable instance object> was printed because there was no mechanism to print the error object. > 2) Given that what I want to happen when the NotYetAllowed() method is > called is: > a) an exception to be raised Happening already. > b) the exception results in a message getting printed; the message > should come from the place where the exception was raised, and it > should be passed to the exception class as a string object; so in this > case the message that should be printed is 'not yet ready' Insert __str__ method as per above. > c) the exception gets handled with the try/except within the > SampleMain class > Now that would be difficult. That 'try' statement is executed when the class is *loaded*. Put a print statement just before the try statement and you'll see what I mean. It's not a declaration that encompasses all calls to methods of instances of the class. Let's take a step back: Why do you think you need to catch and *handle* such exceptions? You have some methods that aren't fit to be seen in public yet? Don't tell anyone about them. Don't put your code in a callable place. Alternatively, put "raise NotImplementedError" -- that's a standard built-in exception -- as the first line of each method. A bit of explanation from you might be useful so that the help can be tailored. And if all you really want to do is just print a message, that's what happens by default e.g. >>> raise NotImplementedError, 'come back after lunch' Traceback (most recent call last): File "<stdin>", line 1, in ? NotImplementedError: come back after lunch >>> class Mistake(Exception): ... pass ... >>> def foo(): ... raise Mistake, 'somebody goofed' ... >>> foo() Traceback (most recent call last): File "<stdin>", line 1, in ? File "<stdin>", line 2, in foo __main__.Mistake: somebody goofed >>> HTH, John -- http://mail.python.org/mailman/listinfo/python-list