Exception Messages

Discussion in 'Python' started by Wanderer, Oct 15, 2012.

  1. Wanderer

    Wanderer Guest

    How do you get Exceptions to print messages? I have an exception defined like this

    class PvCamError(Exception):
    def __init__(self, msg):
    self.msg = msg

    But when the error is raised I get this:

    Traceback (most recent call last):
    File "C:\Python27\lib\site-packages\ipython-0.12.1-py2.7.egg\IPython\core\interactiveshell.py", line 2538, in run_code
    exec code_obj in self.user_global_ns, self.user_ns
    File "<ipython-input-1-fb48da797583>", line 1, in <module>
    import S477Test
    File "U:\workspace\camera\src\S477Test.py", line 13, in <module>
    camera.getSerialNum()
    File "U:\workspace\camera\src\S477.py", line 131, in getSerialNum
    num = self.pl.getParamValue(pvcamConstants.PARAM_HEAD_SER_NUM_ALPHA)
    File "U:\workspace\camera\src\pvcam.py", line 261, in getParamValue
    raise PvCamError("Unhandled Type: {0}".format(attype))
    PvCamError

    Why wasn't the message printed out?
    Wanderer, Oct 15, 2012
    #1
    1. Advertising

  2. Wanderer

    MRAB Guest

    On 2012-10-15 17:00, Wanderer wrote:
    > How do you get Exceptions to print messages? I have an exception defined like this
    >
    > class PvCamError(Exception):
    > def __init__(self, msg):
    > self.msg = msg
    >
    > But when the error is raised I get this:
    >
    > Traceback (most recent call last):
    > File "C:\Python27\lib\site-packages\ipython-0.12.1-py2.7.egg\IPython\core\interactiveshell.py", line 2538, in run_code
    > exec code_obj in self.user_global_ns, self.user_ns
    > File "<ipython-input-1-fb48da797583>", line 1, in <module>
    > import S477Test
    > File "U:\workspace\camera\src\S477Test.py", line 13, in <module>
    > camera.getSerialNum()
    > File "U:\workspace\camera\src\S477.py", line 131, in getSerialNum
    > num = self.pl.getParamValue(pvcamConstants.PARAM_HEAD_SER_NUM_ALPHA)
    > File "U:\workspace\camera\src\pvcam.py", line 261, in getParamValue
    > raise PvCamError("Unhandled Type: {0}".format(attype))
    > PvCamError
    >
    > Why wasn't the message printed out?
    >

    You didn't add a __str__ method:

    class PvCamError(Exception):
    def __init__(self, msg):
    self.msg = msg
    def __str__(self):
    return self.msg
    MRAB, Oct 15, 2012
    #2
    1. Advertising

  3. Wanderer

    John Gordon Guest

    In <> MRAB <> writes:

    > > Why wasn't the message printed out?

    >
    > You didn't add a __str__ method:


    > class PvCamError(Exception):
    > def __init__(self, msg):
    > self.msg = msg
    > def __str__(self):
    > return self.msg


    Wouldn't PvCamError inherit __str__() from Exception?

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Oct 15, 2012
    #3
  4. Wanderer

    MRAB Guest

    On 2012-10-15 17:22, John Gordon wrote:
    > In <> MRAB <> writes:
    >
    >> > Why wasn't the message printed out?

    >>
    >> You didn't add a __str__ method:

    >
    >> class PvCamError(Exception):
    >> def __init__(self, msg):
    >> self.msg = msg
    >> def __str__(self):
    >> return self.msg

    >
    > Wouldn't PvCamError inherit __str__() from Exception?
    >

    Yes, but you've put the message in msg, and Exception doesn't have that
    attribute.

    An alternative approach would be:

    class PvCamError(Exception):
    pass
    MRAB, Oct 15, 2012
    #4
  5. Wanderer

    Wanderer Guest

    On Monday, October 15, 2012 12:34:53 PM UTC-4, MRAB wrote:

    >
    > Yes, but you've put the message in msg, and Exception doesn't have that
    >
    > attribute.
    >


    That's weird. I got this Exception class definition idea from this post by Guido van Rostrum, Where he gives this main function to look like

    import sys
    import getopt

    class Usage(Exception):
    def __init__(self, msg):
    self.msg = msg

    def main(argv=None):
    if argv is None:
    argv = sys.argv
    try:
    try:
    opts, args = getopt.getopt(argv[1:], "h", ["help"])
    except getopt.error, msg:
    raise Usage(msg)
    # more code, unchanged
    except Usage, err:
    print >>sys.stderr, err.msg
    print >>sys.stderr, "for help use --help"
    return 2

    if __name__ == "__main__":
    sys.exit(main())




    http://www.artima.com/weblogs/viewpost.jsp?thread=4829
    Wanderer, Oct 15, 2012
    #5
  6. Wanderer

    Wanderer Guest

    On Monday, October 15, 2012 12:34:53 PM UTC-4, MRAB wrote:

    >
    > Yes, but you've put the message in msg, and Exception doesn't have that
    >
    > attribute.
    >


    That's weird. I got this Exception class definition idea from this post by Guido van Rostrum, Where he gives this main function to look like

    import sys
    import getopt

    class Usage(Exception):
    def __init__(self, msg):
    self.msg = msg

    def main(argv=None):
    if argv is None:
    argv = sys.argv
    try:
    try:
    opts, args = getopt.getopt(argv[1:], "h", ["help"])
    except getopt.error, msg:
    raise Usage(msg)
    # more code, unchanged
    except Usage, err:
    print >>sys.stderr, err.msg
    print >>sys.stderr, "for help use --help"
    return 2

    if __name__ == "__main__":
    sys.exit(main())




    http://www.artima.com/weblogs/viewpost.jsp?thread=4829
    Wanderer, Oct 15, 2012
    #6
  7. Wanderer

    Wanderer Guest

    On Monday, October 15, 2012 1:18:52 PM UTC-4, Wanderer wrote:
    > On Monday, October 15, 2012 12:34:53 PM UTC-4, MRAB wrote:
    >
    >
    >
    > >

    >
    > > Yes, but you've put the message in msg, and Exception doesn't have that

    >
    > >

    >
    > > attribute.

    >
    > >

    >
    >
    >
    > That's weird. I got this Exception class definition idea from this post by Guido van Rostrum, Where he gives this main function to look like
    >
    >
    >
    > import sys
    >
    > import getopt
    >
    >
    >
    > class Usage(Exception):
    >
    > def __init__(self, msg):
    >
    > self.msg = msg
    >
    >
    >
    > def main(argv=None):
    >
    > if argv is None:
    >
    > argv = sys.argv
    >
    > try:
    >
    > try:
    >
    > opts, args = getopt.getopt(argv[1:], "h", ["help"])
    >
    > except getopt.error, msg:
    >
    > raise Usage(msg)
    >
    > # more code, unchanged
    >
    > except Usage, err:
    >
    > print >>sys.stderr, err.msg
    >
    > print >>sys.stderr, "for help use --help"
    >
    > return 2
    >
    >
    >
    > if __name__ == "__main__":
    >
    > sys.exit(main())
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > http://www.artima.com/weblogs/viewpost.jsp?thread=4829


    Oops. I meant Guido van Rossum
    Wanderer, Oct 15, 2012
    #7
  8. Wanderer

    Wanderer Guest

    On Monday, October 15, 2012 1:18:52 PM UTC-4, Wanderer wrote:
    > On Monday, October 15, 2012 12:34:53 PM UTC-4, MRAB wrote:
    >
    >
    >
    > >

    >
    > > Yes, but you've put the message in msg, and Exception doesn't have that

    >
    > >

    >
    > > attribute.

    >
    > >

    >
    >
    >
    > That's weird. I got this Exception class definition idea from this post by Guido van Rostrum, Where he gives this main function to look like
    >
    >
    >
    > import sys
    >
    > import getopt
    >
    >
    >
    > class Usage(Exception):
    >
    > def __init__(self, msg):
    >
    > self.msg = msg
    >
    >
    >
    > def main(argv=None):
    >
    > if argv is None:
    >
    > argv = sys.argv
    >
    > try:
    >
    > try:
    >
    > opts, args = getopt.getopt(argv[1:], "h", ["help"])
    >
    > except getopt.error, msg:
    >
    > raise Usage(msg)
    >
    > # more code, unchanged
    >
    > except Usage, err:
    >
    > print >>sys.stderr, err.msg
    >
    > print >>sys.stderr, "for help use --help"
    >
    > return 2
    >
    >
    >
    > if __name__ == "__main__":
    >
    > sys.exit(main())
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > http://www.artima.com/weblogs/viewpost.jsp?thread=4829


    Oops. I meant Guido van Rossum
    Wanderer, Oct 15, 2012
    #8
  9. Wanderer

    MRAB Guest

    On 2012-10-15 18:18, Wanderer wrote:
    > On Monday, October 15, 2012 12:34:53 PM UTC-4, MRAB wrote:
    >
    >>
    >> Yes, but you've put the message in msg, and Exception doesn't have that
    >>
    >> attribute.
    >>

    >
    > That's weird. I got this Exception class definition idea from this post by Guido van Rostrum, Where he gives this main function to look like
    >
    > import sys
    > import getopt
    >
    > class Usage(Exception):
    > def __init__(self, msg):
    > self.msg = msg
    >
    > def main(argv=None):
    > if argv is None:
    > argv = sys.argv
    > try:
    > try:
    > opts, args = getopt.getopt(argv[1:], "h", ["help"])
    > except getopt.error, msg:
    > raise Usage(msg)
    > # more code, unchanged
    > except Usage, err:
    > print >>sys.stderr, err.msg
    > print >>sys.stderr, "for help use --help"
    > return 2
    >
    > if __name__ == "__main__":
    > sys.exit(main())
    >
    >
    >
    >
    > http://www.artima.com/weblogs/viewpost.jsp?thread=4829
    >
    >

    Note how it explicitly prints err.msg.
    MRAB, Oct 15, 2012
    #9
  10. Wanderer

    Wanderer Guest

    On Monday, October 15, 2012 1:34:24 PM UTC-4, MRAB wrote:
    > On 2012-10-15 18:18, Wanderer wrote:
    >
    > > On Monday, October 15, 2012 12:34:53 PM UTC-4, MRAB wrote:

    >
    > >

    >
    > >>

    >
    > >> Yes, but you've put the message in msg, and Exception doesn't have that

    >
    > >>

    >
    > >> attribute.

    >
    > >>

    >
    > >

    >
    > > That's weird. I got this Exception class definition idea from this post by Guido van Rostrum, Where he gives this main function to look like

    >
    > >

    >
    > > import sys

    >
    > > import getopt

    >
    > >

    >
    > > class Usage(Exception):

    >
    > > def __init__(self, msg):

    >
    > > self.msg = msg

    >
    > >

    >
    > > def main(argv=None):

    >
    > > if argv is None:

    >
    > > argv = sys.argv

    >
    > > try:

    >
    > > try:

    >
    > > opts, args = getopt.getopt(argv[1:], "h", ["help"])

    >
    > > except getopt.error, msg:

    >
    > > raise Usage(msg)

    >
    > > # more code, unchanged

    >
    > > except Usage, err:

    >
    > > print >>sys.stderr, err.msg

    >
    > > print >>sys.stderr, "for help use --help"

    >
    > > return 2

    >
    > >

    >
    > > if __name__ == "__main__":

    >
    > > sys.exit(main())

    >
    > >

    >
    > >

    >
    > >

    >
    > >

    >
    > > http://www.artima.com/weblogs/viewpost.jsp?thread=4829

    >
    > >

    >
    > >

    >
    > Note how it explicitly prints err.msg.


    Not in the raise statement.

    Adding the def __str__ made it work for me.
    Thanks
    Wanderer, Oct 15, 2012
    #10
  11. Wanderer

    Wanderer Guest

    On Monday, October 15, 2012 1:34:24 PM UTC-4, MRAB wrote:
    > On 2012-10-15 18:18, Wanderer wrote:
    >
    > > On Monday, October 15, 2012 12:34:53 PM UTC-4, MRAB wrote:

    >
    > >

    >
    > >>

    >
    > >> Yes, but you've put the message in msg, and Exception doesn't have that

    >
    > >>

    >
    > >> attribute.

    >
    > >>

    >
    > >

    >
    > > That's weird. I got this Exception class definition idea from this post by Guido van Rostrum, Where he gives this main function to look like

    >
    > >

    >
    > > import sys

    >
    > > import getopt

    >
    > >

    >
    > > class Usage(Exception):

    >
    > > def __init__(self, msg):

    >
    > > self.msg = msg

    >
    > >

    >
    > > def main(argv=None):

    >
    > > if argv is None:

    >
    > > argv = sys.argv

    >
    > > try:

    >
    > > try:

    >
    > > opts, args = getopt.getopt(argv[1:], "h", ["help"])

    >
    > > except getopt.error, msg:

    >
    > > raise Usage(msg)

    >
    > > # more code, unchanged

    >
    > > except Usage, err:

    >
    > > print >>sys.stderr, err.msg

    >
    > > print >>sys.stderr, "for help use --help"

    >
    > > return 2

    >
    > >

    >
    > > if __name__ == "__main__":

    >
    > > sys.exit(main())

    >
    > >

    >
    > >

    >
    > >

    >
    > >

    >
    > > http://www.artima.com/weblogs/viewpost.jsp?thread=4829

    >
    > >

    >
    > >

    >
    > Note how it explicitly prints err.msg.


    Not in the raise statement.

    Adding the def __str__ made it work for me.
    Thanks
    Wanderer, Oct 15, 2012
    #11
  12. Wanderer

    Terry Reedy Guest

    On 10/15/2012 12:22 PM, John Gordon wrote:
    > In <> MRAB <> writes:
    >
    >>> Why wasn't the message printed out?

    >>
    >> You didn't add a __str__ method:

    >
    >> class PvCamError(Exception):
    >> def __init__(self, msg):
    >> self.msg = msg
    >> def __str__(self):
    >> return self.msg

    >
    > Wouldn't PvCamError inherit __str__() from Exception?


    Exception instances get a .args attibute set to the arguments of the
    class call and printed in the .__str__ message.

    >>> dir(Exception())

    ['__cause__', '__class__', '__context__', '__delattr__', '__dict__',
    '__dir__', '__doc__', '__eq__', '__format__', '__ge__',
    '__getattribute__', '__gt__', '__hash__', '__init__', '__le__',
    '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
    '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__',
    '__subclasshook__', '__suppress_context__', '__traceback__', 'args',
    'with_traceback']
    >>> Exception('abc', 1)

    Exception('abc', 1)

    So class MyError(Exception) will get that basic default behavior. For
    fancier printing, one needs a fancier .__str__ method ;-).

    --
    Terry Jan Reedy
    Terry Reedy, Oct 15, 2012
    #12
  13. On Mon, 15 Oct 2012 09:00:15 -0700, Wanderer wrote:

    > How do you get Exceptions to print messages? I have an exception defined
    > like this
    >
    > class PvCamError(Exception):
    > def __init__(self, msg):
    > self.msg = msg


    Please don't invent yet another interface for exception messages.
    Exceptions already have two defined interfaces for accessing the error
    message:

    exception.message # older, now obsolete, and gone in Python 3.
    exception.args # recommended way

    Please use the same interface that nearly all exceptions already have and
    use an "args" argument. The easiest way to do this is also the simplest:

    class PvCamError(Exception): pass


    If you *must* support "msg", do something like this:

    class PvCamError(Exception):
    def __init__(self, msg, *args):
    super(PvCamError, self).__init__(msg, *args) # see footnote [1]
    self.msg = msg





    [1] If you're using Python 3, you can abbreviate the super call to:

    super().__init__(self, msg, *args)



    --
    Steven
    Steven D'Aprano, Oct 16, 2012
    #13
    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. =?Utf-8?B?U3JpZGhhcg==?=

    passing exception messages from a dll to a client

    =?Utf-8?B?U3JpZGhhcg==?=, Mar 31, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    375
    Karl Seguin [MVP]
    Apr 3, 2006
  2. Stefan Behnel
    Replies:
    15
    Views:
    513
    Steve Holden
    Oct 23, 2004
  3. gnewsgroup
    Replies:
    2
    Views:
    590
    gnewsgroup
    Sep 22, 2007
  4. TJ
    Replies:
    2
    Views:
    327
  5. shearichard

    PEP8 compliance and exception messages ?

    shearichard, Dec 6, 2010, in forum: Python
    Replies:
    8
    Views:
    599
    shearichard
    Dec 7, 2010
Loading...

Share This Page