I’m doing a review for our codebase, and there are many statements like this:
try
{
doSomething()
} catch (Exception e)
{
}
but I would like a way to know which exception is thrown by doSomething() (there’s no throw statement in the implementation of doSomething) so i can catch that exception instead of just catching Exception in general, even with findBugs it gives a warning REC_CATCH_EXCEPTION.
I should mention that logging the exception or printing it to console will not help me because it takes time in this case to reproduce the error that causes the exception here.
Thanks
If there’s no
throwsstatement indoSomething(e.g.doSomething() throws IOException), any exceptions that will occur will be an instance ofRuntimeException. If you want to know the exact class of an exception thrown bydoSomething, you can always tryKnowing which runtime exceptions can be thrown without actually running the program is difficult. Even if none of the code that
doSomething()calls has an explicit throw, core java operations can always throwNullPointerException,ArrayIndexOutOfBoundsException, etc with the wrong input. Here are some ideas:doSomething.doSomethingshould be ready for.In any case it’s usually a good idea to catch exceptions that are as specific as possible, since you don’t know exactly what went wrong when you try to deal with all cases in one clause.