Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31335

Re: Exception Messages

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Exception Messages
Date 2012-10-15 15:15 -0400
References <8b5cb95f-424b-45a8-b19b-42f92f531a75@googlegroups.com> <mailman.2217.1350317845.27098.python-list@python.org> <k5hd7c$emf$1@reader1.panix.com>
Newsgroups comp.lang.python
Message-ID <mailman.2232.1350328556.27098.python-list@python.org> (permalink)

Show all headers | View raw


On 10/15/2012 12:22 PM, John Gordon wrote:
> In <mailman.2217.1350317845.27098.python-list@python.org> MRAB <python@mrabarnett.plus.com> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Exception Messages Wanderer <wanderer@dialup4less.com> - 2012-10-15 09:00 -0700
  Re: Exception Messages MRAB <python@mrabarnett.plus.com> - 2012-10-15 17:17 +0100
    Re: Exception Messages John Gordon <gordon@panix.com> - 2012-10-15 16:22 +0000
      Re: Exception Messages MRAB <python@mrabarnett.plus.com> - 2012-10-15 17:34 +0100
        Re: Exception Messages Wanderer <wanderer@dialup4less.com> - 2012-10-15 10:18 -0700
          Re: Exception Messages Wanderer <wanderer@dialup4less.com> - 2012-10-15 10:20 -0700
          Re: Exception Messages Wanderer <wanderer@dialup4less.com> - 2012-10-15 10:20 -0700
          Re: Exception Messages MRAB <python@mrabarnett.plus.com> - 2012-10-15 18:34 +0100
            Re: Exception Messages Wanderer <wanderer@dialup4less.com> - 2012-10-15 10:44 -0700
            Re: Exception Messages Wanderer <wanderer@dialup4less.com> - 2012-10-15 10:44 -0700
        Re: Exception Messages Wanderer <wanderer@dialup4less.com> - 2012-10-15 10:18 -0700
      Re: Exception Messages Dave Angel <d@davea.name> - 2012-10-15 12:42 -0400
      Re: Exception Messages Terry Reedy <tjreedy@udel.edu> - 2012-10-15 15:15 -0400
  Re: Exception Messages Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-16 00:47 +0000

csiph-web