How to find out which exception is thrown

I am doing a review for our code base, and there are many such statements:

try { doSomething() } catch (Exception e) { } 

but I would like to know which exception is thrown by doSomething () (there is no throw statement in the doSomething implementation), so I can catch this exception instead of just catching the Exception at all, even with findBugs it gives a warning REC_CATCH_EXCEPTION .

I should mention that registering an exception or printing it on the console will not help me, because in this case it takes time to reproduce the error that causes the exception here.

thanks

+7
source share
4 answers

If doSomething (for example, doSomething() throws IOException ) does not have a throws statement), any exceptions that will occur will be an instance of RuntimeException . If you want to know the exact exception class thrown by doSomething , you can always try

 try { doSomething(); } catch (RuntimeException e){ System.out.println(e.getClass().getName()); } 

Knowing which runtime exceptions may be thrown without actually running the program is difficult. Even if none of the codes called by doSomething() has an explicit throw, java kernel operations can always throw a NullPointerException , ArrayIndexOutOfBoundsException , etc. With the wrong entry. Here are some ideas:

  • Pass by hand. At least you will learn some of the exceptions.
  • Use reflection to find any expression statements available from doSomething .
  • Run the test cases and write down the exceptions mentioned above. Good tests will reveal important errors that should be prepared for doSomething .
  • Go to the people who first laid the trick

In any case, it is generally best to catch exceptions that are as specific as possible, because you don’t know exactly what went wrong when you try to deal with all the cases in one sentence.

+14
source

You can: 1) dig through all the code in doSomething () and everything that it calls to see the exception handling and which RuntimeExceptions you can throw, or 2) take catch (Exception e) and wait for it to fail. This is a problem that checked exceptions tried to overcome by making a strongly typed declaration in the method signatures about which exceptions should be handled as a result of the method call.

+1
source

If the throws does not exist, then the method does not throw any test exceptions. The javadoc of this method can provide information about any unchecked exceptions that the method can use, but this is not necessary.

Why do you want to catch any exceptions at all?

0
source

Do you also want to catch exceptions at compile time or exceptions at run time? If you only want to catch the compilation, just delete the current catch block - you will immediately receive error messages stating that some exceptions do not get caught. After you add one exception, you will see error messages for others - you can finish the search for all possible exceptions created by your code block.

0
source

All Articles