How can I catch all exception in python?

Discussion in 'Python' started by yinglcs@gmail.com, Mar 27, 2007.

  1. Guest

    , Mar 27, 2007
    #1
    1. Advertisements

  2. Guest

    On Mar 27, 1:09 pm, "" <> wrote:
    > I read the document here about exception handling in python:
    >
    > http://www.diveintopython.org/file_handling/index.html
    >
    > Can you please tell me how can I catch all exception in python?
    > like this in Java:
    > try {
    > ....
    >
    > } catch (Throwable t) {
    > ...
    > }


    Technically speaking, you can catch all errors as follows:

    try:
    # do something
    except Exception, e:
    print e


    However, this is NOT the recommended way of handling errors. Typically
    you catch only expected errors, such as when you open a file, you
    check for an IOError. By catching all errors, you will learn less and
    likely have hard-to-understand bugs in your program.

    Mike
     
    , Mar 27, 2007
    #2
    1. Advertisements

  3. En Tue, 27 Mar 2007 15:09:18 -0300, <>
    escribió:

    > I read the document here about exception handling in python:
    >
    > http://www.diveintopython.org/file_handling/index.html
    >
    > Can you please tell me how can I catch all exception in python?
    > like this in Java:
    > try {
    > ....
    > } catch (Throwable t) {
    > ...
    > }


    See the Further Reading section on that same page.
    Exceptions are covered in the Python Tutorial here:
    http://docs.python.org/tut/node10.html

    --
    Gabriel Genellina
     
    Gabriel Genellina, Mar 27, 2007
    #3
  4. Guest

    On Mar 27, 9:15 pm, 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)
     
    , Mar 27, 2007
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. 8519428 Wang Tian-da Alex
    Replies:
    3
    Views:
    1,619
    Mike Wahler
    Dec 2, 2003
  2. John Black
    Replies:
    8
    Views:
    4,512
    Xenos
    Aug 20, 2004
  3. Lucas z Izbanerowic

    An exception said: catch me if you can

    Lucas z Izbanerowic, Mar 21, 2005, in forum: C++
    Replies:
    0
    Views:
    399
    Lucas z Izbanerowic
    Mar 21, 2005
  4. chris
    Replies:
    4
    Views:
    865
    yuvalif
    Nov 15, 2005
  5. Replies:
    3
    Views:
    420
  6. =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z

    Why can not catch the inner exception

    =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z, Jun 7, 2007, in forum: Python
    Replies:
    2
    Views:
    463
    Guest
    Jun 7, 2007
  7. Marteno Rodia

    catch doesn't catch a thrown exception

    Marteno Rodia, Aug 3, 2009, in forum: Java
    Replies:
    5
    Views:
    818
    Daniel Pitts
    Aug 5, 2009
  8. Leslie Viljoen

    Exception catch-all

    Leslie Viljoen, Nov 8, 2006, in forum: Ruby
    Replies:
    4
    Views:
    162
    Roger Pack
    Oct 1, 2007
Loading...