How can I obtain the exception object on a generlized except statement?

John Machin sjmachin at lexicon.net
Sun Jun 10 08:33:28 EDT 2007


On Jun 10, 10:13 pm, "Diez B. Roggisch" <d... at nospam.web.de> wrote:
> Chris Allen schrieb:
>
>
>
> > I am confused on one aspect of exception handling.  If you specify the
> > exception object type to match in an except statement it is possible
> > to also obtain the exception object itself, but I can't figure out how
> > to get the exception object when I don't specify a match.
>
> > for example:
>
> >>>> try: urlopen('http://www.google.com')
> >>>> except socket.error, msg:
> >>>>     print str(msg)
>
> > this works and I can do what I like with the exception object (msg).
> > but I can't do this with a simple except statment.
>
> >>>> except msg:
>
> > this won't work because it will think msg is the type to match
>
> >>>> except ,msg:
>
> > syntax error
>
> >>>> except *,msg:
>
> > syntax error
>
> >>>> except (),msg:
>
> > Hmm I didn't try that one before I started this post.  It doesn't give
> > me a syntax error.  I'll experiment.
> > Even if the above syntax works, is this the way to do this?  It seems
> > sort of funky.
>
> > How do I do this?  Thanks.
>
> pydoc sys.exc_info
>

In particular, you want sys.exc_info()[:2]







More information about the Python-list mailing list