1

Is there a way to take exception handling output from an imported module and process it from the calling program? For example, I have an imported module that writes an HTTP exception

except urllib2.HTTPError, e:
   sys.stderr.write(str(e) + '\n')

If a 404 occurs, then the calling programing only sees the following:

HTTP Error 404: not found

Can this be taken as input without modifying the imported module? I would need to perform different tasks depending on the HTTP Error that is returned.

3
  • sys.stderr is just a file-like object, so before calling the module code you could record the position in the stream and after calling that piece of code just read it from that position to the end. Commented May 9, 2014 at 19:50
  • What is the imported module? It looks to me that it should re-raise the exception, rather than just log it but otherwise ignore it. Commented May 9, 2014 at 19:57
  • its a custom 3rd party module. The exception is raised by the imported program and then ignored by the calling program. this is normal, but I want to capture it in the calling program. Not sure how to implement Midnighter's suggestion Commented May 9, 2014 at 20:05

1 Answer 1

1

If you can modify the imported module, raise the error in the except block like so:

except urllib2.HTTPError, e:
    sys.stderr.write(str(e) + '\n')
    raise e

Then in the calling program, catch the error and inspect it for the error code:

except urllib2.HTTPError, e:
    if e.code == 404:
        do_something_here()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.