How can I catch all exception in python?

Jeff McNeil jeff at jmcneil.net
Tue Mar 27 17:01:31 EDT 2007


You could also use sys.excepthook if you're trying to handle uncaught
exceptions.



On 27 Mar 2007 11:45:54 -0700, irstas at gmail.com <irstas at gmail.com> wrote:
>
> On Mar 27, 9:15 pm, kyoso... at gmail.com wrote:
> > Technically speaking, you can catch all errors as follows:
> >
> > try:
> >    # do something
> > except Exception, e:
> >    print e
>
> That won't catch exceptions/errors that don't derive from
> Exception class. For example a string won't be caught:
>
> try:
>    raise "foo"
> except Exception, e:
>    print e
>
> But this will catch all exceptions:
>
> try:
>    raise "foo"
> except:
>    print sys.exc_info()
>
> (there may be other ways I don't know of)
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070327/28862966/attachment.html>


More information about the Python-list mailing list