Jeff Jeff - 2 years ago 62
Java Question

If a call is in a try/catch block and the method also throws the exception, will the catch have precedence?

If the method is declared to throw the same exceptions thrown by some code and that code is also enclosed in a try/catch, will the exception be caught by the catch or will the error still be thrown? I am guessing that the catch has precedence although I am not 100% sure.

Answer Source

If I understand you correctly, you are asking:

void someMethod() throws SomeException {    
  try {
  } catch (SomeException e) {
    // is this reached or does it throw from the method?

The catch clause will be triggered and the exception is considered handled. Unless you re-throw it from that block, it will not escape the method.

In my example, there is no need for your method to declare that it throws SomeException, because it doesn't.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download