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
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
Related Q & A:
- How do I throw an exception when I receive a signal?Best solution by stackoverflow.com
- How to add a MIME handler to Firefox and Internet Explorer from an installer?Best solution by Stack Overflow
- Is UPS Part time Package handler a good job?Best solution by Yahoo! Answers
- Is there a catch to this $1,200 apartment/small home in East Flatbush?Best solution by Yahoo! Answers
- Is it really tough to work as a package handler at ups?Best solution by Quora
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.