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. Advertising

  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. Advertising

  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. Advertising

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. John Black
    Replies:
    8
    Views:
    4,160
    Xenos
    Aug 20, 2004
  2. chris
    Replies:
    4
    Views:
    756
    yuvalif
    Nov 15, 2005
  3. Adam
    Replies:
    9
    Views:
    576
    red floyd
    Feb 2, 2006
  4. Marteno Rodia

    catch doesn't catch a thrown exception

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

    Exception catch-all

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

Share This Page