470,848 Members | 2,210 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

Re: User-defined Exceptions: is self.args OK?

En Thu, 17 Apr 2008 19:11:36 -0300, Petr Jake¨ <pe********@tpc.cz>
escribió:
I am trying to dig through User-defined Exceptions. chapter 8.5 in
http://docs.python.org/tut/node10.html
I would like to know, if is it OK to add following line to the __init__
method of the TransitionError class?

self.args = (self.previous, self.next, self.message)
Not necesarily, but you should call the base __init__, perhaps with a
suitable message.

Most of the time, a meaningful error message is all what I want, so I
write:

class FooError(Exception): pass

(perhaps inheriting from ValueError or TypeError or any other more
meaningful base error).
Then, when something wrong is detected:

if ....:
raise FooError, "some %s message" % foo

In your case, you appear to require more info stored in the exception. Try
this (based on your code):

class TransitionError(Error): # I assume Error inherits from Exception?
def __init__(self, previous, next, message):
self.previous = previous
self.next = next
Error.__init__(self, previous, next, message)
# perhaps: Error.__init__(self, message)
# if message already contain references to
# previous and next

raise TransitionError, (1, 2, "oops")

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
__main__.TransitionError: (1, 2, 'oops')

--
Gabriel Genellina

Jun 27 '08 #1
0 687

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

60 posts views Thread by Fotios | last post: by
3 posts views Thread by zlst | last post: by
6 posts views Thread by martin | last post: by
2 posts views Thread by rn5a | last post: by
1 post views Thread by Carlettus | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.