Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #31315 > unrolled thread
| Started by | Wanderer <wanderer@dialup4less.com> |
|---|---|
| First post | 2012-10-15 09:00 -0700 |
| Last post | 2012-10-16 00:47 +0000 |
| Articles | 14 — 6 participants |
Back to article view | Back to comp.lang.python
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
| From | Wanderer <wanderer@dialup4less.com> |
|---|---|
| Date | 2012-10-15 09:00 -0700 |
| Subject | Exception Messages |
| Message-ID | <8b5cb95f-424b-45a8-b19b-42f92f531a75@googlegroups.com> |
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?
[toc] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2012-10-15 17:17 +0100 |
| Message-ID | <mailman.2217.1350317845.27098.python-list@python.org> |
| In reply to | #31315 |
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
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2012-10-15 16:22 +0000 |
| Message-ID | <k5hd7c$emf$1@reader1.panix.com> |
| In reply to | #31317 |
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?
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2012-10-15 17:34 +0100 |
| Message-ID | <mailman.2218.1350318893.27098.python-list@python.org> |
| In reply to | #31318 |
On 2012-10-15 17:22, 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?
>
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
[toc] | [prev] | [next] | [standalone]
| From | Wanderer <wanderer@dialup4less.com> |
|---|---|
| Date | 2012-10-15 10:18 -0700 |
| Message-ID | <96889d3a-1217-4743-93b5-f8dd5686eebd@googlegroups.com> |
| In reply to | #31319 |
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
[toc] | [prev] | [next] | [standalone]
| From | Wanderer <wanderer@dialup4less.com> |
|---|---|
| Date | 2012-10-15 10:20 -0700 |
| Message-ID | <8945f3ba-7acc-402b-b3f5-43caa2e2cde9@googlegroups.com> |
| In reply to | #31324 |
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
[toc] | [prev] | [next] | [standalone]
| From | Wanderer <wanderer@dialup4less.com> |
|---|---|
| Date | 2012-10-15 10:20 -0700 |
| Message-ID | <mailman.2224.1350321609.27098.python-list@python.org> |
| In reply to | #31324 |
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
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2012-10-15 18:34 +0100 |
| Message-ID | <mailman.2226.1350322455.27098.python-list@python.org> |
| In reply to | #31324 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Wanderer <wanderer@dialup4less.com> |
|---|---|
| Date | 2012-10-15 10:44 -0700 |
| Message-ID | <1994ef31-be75-457f-a9d5-706a8e3d6c1b@googlegroups.com> |
| In reply to | #31329 |
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
[toc] | [prev] | [next] | [standalone]
| From | Wanderer <wanderer@dialup4less.com> |
|---|---|
| Date | 2012-10-15 10:44 -0700 |
| Message-ID | <mailman.2227.1350323056.27098.python-list@python.org> |
| In reply to | #31329 |
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
[toc] | [prev] | [next] | [standalone]
| From | Wanderer <wanderer@dialup4less.com> |
|---|---|
| Date | 2012-10-15 10:18 -0700 |
| Message-ID | <mailman.2223.1350321541.27098.python-list@python.org> |
| In reply to | #31319 |
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
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <d@davea.name> |
|---|---|
| Date | 2012-10-15 12:42 -0400 |
| Message-ID | <mailman.2219.1350319399.27098.python-list@python.org> |
| In reply to | #31318 |
On 10/15/2012 12:34 PM, MRAB wrote: > On 2012-10-15 17:22, 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? >> > 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 > Wouldn't another approach be for this __init__() method to call the base-class __init__() method with whatever data it needs? -- DaveA
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-10-15 15:15 -0400 |
| Message-ID | <mailman.2232.1350328556.27098.python-list@python.org> |
| In reply to | #31318 |
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
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-10-16 00:47 +0000 |
| Message-ID | <507cae95$0$6574$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #31315 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web