Best practice for handling exceptions from libraries imported by other libraries in Python?

I think you answered you question yourself. Import urllib2 and catch the exception in your module.

I think you answered you question yourself. Import urllib2 and catch the exception in your module. From urllib2 import URLError try: # something except URLError, e: # Do something in case of error.

This is what I figured (and have been doing). I guess I thought there was a more "pythonic" way to handle this. It would seem that I should probably also not worry about what threw the exception and just catch any exception thrown from the library (pycontrol) call I am trying to use.

– perfectfromnowon Jun 18 at 5:22 Not quite -- only catch exceptions you are prepared to deal with. If you can't do anything useful with, for example, ValueError there is no point in catching it. – Ethan Furman Jul 29 at 22:23.

After all, any exception (not only URLError) raised from be = pc. BIGIP(...) means you cannot go on. I suggest: import traceback try: be = pc.

BIGIP(...) except: traceback. Print_exc() exitcode = 1 else: do_something_with_connection(b) Another idea: Why bother with catching the exception at all? The Python interpreter will dump a stack trace to stderr and exit the program when it encounters an unhandled exception: be = bc.

BIGIP(...) do_something_with_connection(b) Or if you need to write to an error log: import logging import sys def main(): be = bc. BIGIP(...) do_something_with_connection(b) if __name__ == "__main__": try: main() except: logging. Exception("An unexpected error occured") sys.

Exit(1).

Thanks for the great suggestions. The exceptions themselves actually communicate useful information in regards to what happened with the remote API. Sometimes the exceptions are actually non-fatal and I just need to do something logical when the remote API operation fails.

– perfectfromnowon Jun 18 at 5:19.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions