S2221 and background thread

Hi.

I would like to discuss validity of this rule in my case:

“Exception” should not be caught when not required by called methods

In general I understand what is it for, but in my situation it seems it shouldn’t be fired (or I’m doing something in a bad way).
I have a Swing application in which I use SwingWorker.
It performs some calculations and calls other part to update GUI.
The thing is, I already had several situations when the exception thrown from done() method was silently ignored - the thread was just finishing. That’s why I added catching Exception there. It’s the last part of this thread task, and I just want to log this exception, so I (or the user) know that something went wrong.
And it’s hard to predict what may be thrown there (I hope nothing ;).
It looks for me like it’s impossible. It’s just I would always like to be informed about such cases.

Hi Ewa,
SwingWorker#done doesn’t have any exception in the method signature. It means it may throw:

In your case (you catch Exception) just change from Exception to RuntimeException. There is no need to catch Exception if only RuntimeException could be thrown.

Cheers

1 Like