PEP 8: mention bare 'except:'

http://www.python.org/sf/411881 is a bug about removing use of 'except:' in stdlib code. In many cases the intent is to catch one particular exception such as ImportError or AttributeError, but catching all exceptions can disguise other problems. Should PEP 8 mention this issue? Here's some proposed text for discussion: - 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 StandardError:'. A good rule of thumb is that you should only use 'except:' if the exception handler will be printing out or logging the traceback; at least the user will be aware that an error has occurred. --amk

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Dec 22, 2006, at 8:05 AM, A.M. Kuchling wrote:
A good rule of thumb is that you should only use 'except:' if the exception handler will be printing out or logging the traceback; at least the user will be aware that an error has occurred.
Another aspect of bare except (IME) is that they are used when you want to do some work, but continue to propagate the exception upwards via a bare raise. E.g. rolling back a transaction, or as you mention, logging the error and continuing. - -Barry -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (Darwin) iQCVAwUBRYv7znEjvBPtnXfVAQKjYAP/fxLKXLYQvA/CAfYKOVhBkeIUdqJ7qyxH fv6PXuOM7RwS4FuwtmmbHOw9omyO0Kv+iddx/MD+2hJN9TeeLveGSyr4kEDG5cYD APhy8KW18bKhtwAiZbxzV4VQ3Q22IQah/nHEmzUfaRYAl8Dms9FjwIPIrlEY2yaS TY+MNrUum2Y= =CZuk -----END PGP SIGNATURE-----

On 12/22/06, A.M. Kuchling <amk@amk.ca> wrote:
http://www.python.org/sf/411881 is a bug about removing use of 'except:' in stdlib code. In many cases the intent is to catch one particular exception such as ImportError or AttributeError, but catching all exceptions can disguise othter problems.
Should PEP 8 mention this issue?
Yes. -Brett
participants (3)
-
A.M. Kuchling
-
Barry Warsaw
-
Brett Cannon