40.8 Common Pitfalls: Swallowing Exceptions and Overly Broad Catches
While exception handling is a cornerstone of robust Python programming, its power comes with significant responsibility. Misusing try blocks, particularly by “swallowing” exceptions or catching errors that are too broad, can create insidious bugs that are notoriously difficult to debug. These practices mask failures, violate the principle of failing fast, and can leave your application in an inconsistent state. The Peril of the Bare except: Clause The most egregious form of exception swallowing is the use of a bare except: clause. This construct catches every exception that derives from BaseException, which includes not only Exception (the typical base class for application errors) but also SystemExit and KeyboardInterrupt. Catching these latter exceptions can prevent a user from exiting your program gracefully with Ctrl-C, leading to a frustrating experience.