How to implicitly check method arguments in Python?

Is a "catch all" exception handler *ever* acceptable?

  • This is a stylistic question rather than a practical one. I've written a bit of code (for purely internal purposes) which does something like this: def do_one(args): component, targ = args try: results = deploy[component](targ) except Exception, e: exception("Exception raised in do_one(%s): %s" % (targ, e)) results = (targ, False) info("do_one(%s) Completed" % targ) return results (where exception and info are aliased to the appropriate methods in a logging instance and the deploy is a dictionary containing the names of different server components as keys and corresponding deployment functions (mostly Pexpect and subprocess.Popen code) as values. do_one() is called by the multiprocessing.Pool.map_async() method as follows: pool = multiprocessing.Pool(processes=options.numjobs) results = pool.map_async(do_one, ((options.component, x ) for x in args)) while not results.ready(): process_log_messages(handler) debug("Waiting for results") results.wait(3) (For now each invocation of the code can only handle a homogenous mapping of targets to component.  So that's set via command line option.  I may add additional code later to look up each target in an external datastore to map it to the component that's supposed to be installed on it). The logging instance is using a "QueueHandler" (pasted in from Python 3.2) and only the master process is posting things from that logging queue to the console (based on verbosity which is set in the options via the command line as handled by the standard library OptionsParser(). My argument is that the map_async() is calling functions in the pool as something like remote procedure calls and thus that those processes should not be allowed to throw their own exceptions.  Those should, in my opinion, be logged and handled in the master/dispatcher process. However, I'm open to counter-arguments and ideas for better handling of them (other than merely logging them).

  • Answer:

    Most Python style questions are best answered by http://www.python.org/dev/peps/pep-0008/, the semi-official Python style guide.  About bare except: clauses, PEP 8 has this to say: When catching exceptions, mention specific exceptions whenever possible instead of using a bare ‘except:’ clause. For example, use: try: import platform_specific_module except ImportError: platform_specific_module = None A bare ‘except:’ clause will catch SystemExit and KeyboardInterrupt exceptions, making it harder to interrupt a program with Control-C, and can disguise other problems.  If you want to catch all exceptions that signal program errors, use ‘except Exception:’. A good rule of thumb is to limit use of bare ‘except’ clauses to two cases: If the exception handler will be printing out or logging the traceback; at least the user will be aware that an error has occurred. If the code needs to do some cleanup work, but then lets the exception propagate upwards with ‘raise’.  ‘try…finally’ is a better way to handle this case.

Anders Kaseorg at Quora Visit the source

Was this solution helpful to you?

Other answers

This is not a Python-specific answer. Yes, but rarely. One case is where you're logging or printing the exception, then raising it again. I'm working on some tools to improve Clojure's debugging story and the catch-all-raise-again pattern (on the JVM, that's catching Throwable, which includes Errors you should never catch without rethrowing) is one I've used quite a bit.

Michael O. Church

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.