# HG changeset patch # User Olivier Delalleau # Date 1314898515 14400 # Node ID 0f326860210e71889437bb06b928428fef6b60dc # Parent 3d4615ee96a4fa0772a38bd4b7d4a29527f2ecd0# Parent 93b8373c6735039a5b8299c6e0d0c0985a141c76 Merged diff -r 93b8373c6735 -r 0f326860210e doc/v2_planning/API_coding_style.txt --- a/doc/v2_planning/API_coding_style.txt Mon Aug 22 11:28:48 2011 -0400 +++ b/doc/v2_planning/API_coding_style.txt Thu Sep 01 13:35:15 2011 -0400 @@ -277,6 +277,23 @@ n_samples_per_split = n_samples / n_splits mean_x = sum(x) / len(x) + * If you really have to catch all exceptions, in general you should use + ``except Exception:`` rather than ``except:``, as the latter also catches + interrupts like when hitting Ctrl-C. + + .. code-block:: python + + # Good (assuming you *must* be catching all exceptions). + try: + someting_that_may_fail_in_unexpected_ways() + except Exception: + do_something_if_it_failed() + # Bad. + try: + someting_that_may_fail_in_unexpected_ways() + except: + do_something_if_it_failed() + * Use either ``try ... except`` or ``try ... finally``, but do not mix ``except`` with ``finally`` (which is not supported in Python 2.4). You can however embed one into the other to mimic the ``try ... except ... @@ -287,14 +304,14 @@ # Good. try: try: - something_that_may_fail() + something_that_may_fail_with_some_known_error() except SomeError: do_something_if_it_failed() finally: always_do_this_regardless_of_what_happened() # Bad. try: - something_that_may_fail() + something_that_may_fail_with_some_known_error() except SomeError: do_something_if_it_failed() finally: