S1181 (catch Throwable) is too broad?

(Maksim Gumerov) #1

Hi, I feel that S1181 Java rule regarding catching Throwable should be reconsidered. The statement itself seems correct; however, in some cases it’s per method contract that it “throws Throwable”. However bad design that is, it’s given, it’s not part of our code and we cannot make it any better - if we want to catch any checked exception, then any possible custom direct descendants of Object+Throwable are also by definition “checked”, and we have no choice than to write “catch Throwable”.

Hence, it’s not bad practice to write “catch Throwable” if the try statement body can potentially throw any Throwable. Rather, it may be considered bad practice to “throws Throwable” (unless the method actually throws custom Throwables); but that’s another rule, and it will only be triggered if such declaration exists in analyzed code, not some external library.