How to catch 'error' type exceptions

Discussion in 'Python' started by Anand Pillai, Jun 30, 2003.

  1. Anand Pillai

    Anand Pillai Guest

    Hi

    I am quite familiar with normal python errors which can
    be caught by using the try... except... finally clause. But
    very often I find other kinds of exceptions raised in my programs.

    Here is an example.

    <TRACEBACK>
    Traceback (most recent call last):
    File "D:\Python22\lib\threading.py", line 414, in __bootstrap
    self.run()
    File "WebUrlTracker.py", line 213, in run
    self.action()
    File "WebUrlTracker.py", line 207, in action
    self.downloadUrl()
    File "WebUrlTracker.py", line 249, in downloadUrl
    self.fetchUrl()
    File "WebUrlTracker.py", line 319, in fetchUrl
    data=self._connection.fetchData(fetchurl)
    File "WebUrlConnector.py", line 267, in fetchData
    connection.request("GET", relpath)
    File "D:\Python22\lib\httplib.py", line 702, in request
    self._send_request(method, url, body, headers)
    File "D:\Python22\lib\httplib.py", line 724, in _send_request
    self.endheaders()
    File "D:\Python22\lib\httplib.py", line 696, in endheaders
    self._send_output()
    File "D:\Python22\lib\httplib.py", line 582, in _send_output
    self.send(msg)
    File "D:\Python22\lib\httplib.py", line 549, in send
    self.connect()
    File "D:\Python22\lib\httplib.py", line 789, in connect
    error: (10060, 'Operation timed out')
    </TRACEBACK>

    If I try to catch this using the try... except clause it does not work
    (actually it raises another error for trying to catch 'error'), i.e the
    following code fails.

    try:
    <Exception generating code>
    except error, e:
    print e

    Could anyone tell me more about these errors and how to deal with
    them ? Probably it is already documented in the python reference, but
    I have missed it in that case.

    Thanks

    Anand Pillai
     
    Anand Pillai, Jun 30, 2003
    #1
    1. Advertising

  2. Anand Pillai

    Alan Kennedy Guest

    Anand Pillai wrote:

    > <TRACEBACK>


    [ Traceback elided ]

    > File "D:\Python22\lib\httplib.py", line 789, in connect
    > error: (10060, 'Operation timed out')
    > </TRACEBACK>
    >
    > If I try to catch this using the try... except clause it does not work
    > (actually it raises another error for trying to catch 'error'), i.e the
    > following code fails.
    >
    > try:
    > <Exception generating code>
    > except error, e:
    > print e


    import socket

    try:
    <Exception generating code>
    except socket.error, e:
    print e

    HTH,

    --
    alan kennedy
    -----------------------------------------------------
    check http headers here: http://xhaus.com/headers
    email alan: http://xhaus.com/mailto/alan
     
    Alan Kennedy, Jun 30, 2003
    #2
    1. Advertising

  3. Your problem is that "error" is not a valid exception type in Python.
    You "should" be trying to catch a specific problem so that you can
    handle it appropriately (such as socket.error as mentioned by another
    poster).

    The lazy, dangerous way would be:

    try:
    # some error-generating code
    except Exception, reason:
    print reason

    but that's not recommended good coding practice.

    Kevin.

    (Anand Pillai) wrote in message
    news:<>...
    > Hi
    >
    > I am quite familiar with normal python errors which can
    > be caught by using the try... except... finally clause. But
    > very often I find other kinds of exceptions raised in my programs.
    >
    > Here is an example.
    >
    > <TRACEBACK>
    > Traceback (most recent call last):
    > File "D:\Python22\lib\threading.py", line 414, in __bootstrap
    > self.run()
    > File "WebUrlTracker.py", line 213, in run
    > self.action()
    > File "WebUrlTracker.py", line 207, in action
    > self.downloadUrl()
    > File "WebUrlTracker.py", line 249, in downloadUrl
    > self.fetchUrl()
    > File "WebUrlTracker.py", line 319, in fetchUrl
    > data=self._connection.fetchData(fetchurl)
    > File "WebUrlConnector.py", line 267, in fetchData
    > connection.request("GET", relpath)
    > File "D:\Python22\lib\httplib.py", line 702, in request
    > self._send_request(method, url, body, headers)
    > File "D:\Python22\lib\httplib.py", line 724, in _send_request
    > self.endheaders()
    > File "D:\Python22\lib\httplib.py", line 696, in endheaders
    > self._send_output()
    > File "D:\Python22\lib\httplib.py", line 582, in _send_output
    > self.send(msg)
    > File "D:\Python22\lib\httplib.py", line 549, in send
    > self.connect()
    > File "D:\Python22\lib\httplib.py", line 789, in connect
    > error: (10060, 'Operation timed out')
    > </TRACEBACK>
    >
    > If I try to catch this using the try... except clause it does not work
    > (actually it raises another error for trying to catch 'error'), i.e the
    > following code fails.
    >
    > try:
    > <Exception generating code>
    > except error, e:
    > print e
    >
    > Could anyone tell me more about these errors and how to deal with
    > them ? Probably it is already documented in the python reference, but
    > I have missed it in that case.
    >
    > Thanks
    >
    > Anand Pillai
     
    Kevin Cazabon, Jun 30, 2003
    #3
  4. On Mon, 30 Jun 2003 14:21:58 +0100, Alan Kennedy <> wrote:

    >Anand Pillai wrote:
    >
    >> <TRACEBACK>

    >
    >[ Traceback elided ]
    >
    >> File "D:\Python22\lib\httplib.py", line 789, in connect
    >> error: (10060, 'Operation timed out')
    >> </TRACEBACK>
    >>
    >> If I try to catch this using the try... except clause it does not work
    >> (actually it raises another error for trying to catch 'error'), i.e the
    >> following code fails.
    >>
    >> try:
    >> <Exception generating code>
    >> except error, e:
    >> print e

    >
    >import socket
    >
    >try:
    > <Exception generating code>
    >except socket.error, e:
    > print e
    >
    >HTH,


    Sometimes a catchall is desirable, e.g. (untested):

    try:
    <Exception generating code>
    except Exception, e:
    print '%s: %s' % (e.__class__.__name__, e)
    if isinstance(e, SystemExit): raise # take the exit
    except:
    print 'Nonstandard Exception %r: %r' % __import__('sys').exc_info()[:2]

    HTH2 ;-)

    Regards,
    Bengt Richter
     
    Bengt Richter, Jul 1, 2003
    #4
  5. On 30 Jun 2003 13:22:32 -0700, (Kevin Cazabon) wrote:

    >Your problem is that "error" is not a valid exception type in Python.
    >You "should" be trying to catch a specific problem so that you can
    >handle it appropriately (such as socket.error as mentioned by another
    >poster).
    >
    >The lazy, dangerous way would be:
    >
    >try:
    > # some error-generating code
    >except Exception, reason:
    > print reason
    >
    >but that's not recommended good coding practice.
    >

    Yes, certainly not internally, unless re-raising all or selected exceptions,
    but as an outside wrapper to a whole app, why not? (You could make traceback
    printing depend on __debug__ or some other option if desired).

    You could also detect and eliminate redundant repetition in a traceback print
    of a recursion limit exception. (I think that would be a nice default, BTW).

    Regards,
    Bengt Richter
     
    Bengt Richter, Jul 1, 2003
    #5
    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. Alan Silver

    How do I catch unhandled exceptions

    Alan Silver, Mar 20, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    1,734
    Alan Silver
    Mar 23, 2006
  2. Chris Berg

    How to catch multiple Exceptions

    Chris Berg, Jan 19, 2004, in forum: Java
    Replies:
    8
    Views:
    15,708
    Chris Berg
    Jan 19, 2004
  3. John Black
    Replies:
    8
    Views:
    4,163
    Xenos
    Aug 20, 2004
  4. Adam
    Replies:
    9
    Views:
    577
    red floyd
    Feb 2, 2006
  5. Marteno Rodia

    catch doesn't catch a thrown exception

    Marteno Rodia, Aug 3, 2009, in forum: Java
    Replies:
    5
    Views:
    572
    Daniel Pitts
    Aug 5, 2009
Loading...

Share This Page