newbie question - exception processing

M

mirandacascade

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

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

My questions are:
1) What is causing the error described in the Traceback?
2) Given that what I want to happen when the NotYetAllowed() method is
called is:
a) an exception to be raised
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'
c) the exception gets handled with the try/except within the
SampleMain class

My question is: what changes must I make to the code to make that
happen?

Thank you.
 
M

M.E.Farmer

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

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

My questions are:
1) What is causing the error described in the Traceback?
2) Given that what I want to happen when the NotYetAllowed() method is
called is:
a) an exception to be raised
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'
c) the exception gets handled with the try/except within the
SampleMain class

My question is: what changes must I make to the code to make that
happen?

Thank you.
Hello ,
This is originally snagged from the standard library.
Spend time reading thru the modules they will show you how to do a lot
of things.
# Exception example
class PSCError(Exception):
# Base for custom errors
def __init__(self, msg=''):
self._msg = msg
Exception.__init__(self, msg)
def __repr__(self):
return self._msg
__str__ = __repr__

class PathError(PSCError):
def __init__(self, msg):
PSCError.__init__(self,
'Path error! : %s'% msg)

class InputError(PSCError):
def __init__(self, msg):
PSCError.__init__(self,
'Input error! : %s'% msg)

# and you use it like this
raise PathError, 'Please check path'
raise InputError, 'Improper input try again'

hth,
M.E.Farmer
 
J

John Machin

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

My questions are:
1) What is causing the error described in the Traceback?

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.
Traceback (most recent call last):
.... pass
........ raise Mistake, 'somebody goofed'
....Traceback (most recent call last):
File "<stdin>", line 1, in ?


HTH,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top