newbie question - exception processing

Discussion in 'Python' started by mirandacascade@yahoo.com, Feb 20, 2005.

  1. Guest

    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 () -
    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?
    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.
     
    , Feb 20, 2005
    #1
    1. Advertising

  2. M.E.Farmer Guest

    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
    >
    > 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 ()

    -
    > 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?
    > 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
     
    M.E.Farmer, Feb 20, 2005
    #2
    1. Advertising

  3. John Machin Guest

    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 ()

    -
    > 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
     
    John Machin, Feb 20, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sharon
    Replies:
    0
    Views:
    386
    Sharon
    Jun 16, 2004
  2. Hubert Hung-Hsien Chang
    Replies:
    2
    Views:
    524
    Michael Foord
    Sep 17, 2004
  3. Replies:
    4
    Views:
    324
    Dennis Lee Bieber
    Oct 5, 2005
  4. philbo30
    Replies:
    12
    Views:
    467
    Keith Thompson
    Jun 7, 2007
  5. Martin Welch
    Replies:
    4
    Views:
    546
    Martin Welch
    Sep 7, 2007
Loading...

Share This Page