Exception Messages
Wanderer
wanderer at dialup4less.com
Mon Oct 15 13:44:07 EDT 2012
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
More information about the Python-list
mailing list