Jeff
Jeff

Reputation: 1591

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.

Upvotes: 1

Views: 3050

Answers (3)

Stephen C
Stephen C

Reputation: 718788

It is not a matter of one construct "taking precedence" over the other. They are saying different things.

(Borrowing @Duncan's example ...)

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

Also, I will assume that SomeException is a checked exception.

This line here:

void someMethod() throws SomeException {

is saying that someMethod >>could<< throw SomeException. This is the contract: what the caller of the method needs to allow for.

It is not saying that it >>does<< throw the exception. What actually happens is determined by the behavior of the body of the method. In this example, the exception is being caught and handled, so we can say that even though the someMethod >>could<< throw the exception, in fact it >>does not<<.

OK ... but that is not the end of the story.

  • What if we changed the body of the method to not catch the exception? Now the ">>does not<<" becomes ">>could<<" ... depending on the signature for doSomethingElse() and its behavior.

  • What if we created a subclass that overrides someMethod with a different behavior. Now, the actual class of the target object will influence whether the exception can happen or not.

But through all of this, the meaning of throws SomeException in the method declaration remains the same. It tells the compiler that whenever this method is called, the caller must deal with the possibility of the checked exception ... even if there is no way that the code as currently written can propagate that exception.

To recap. Neither "takes precedence". They are saying different things.

Upvotes: 0

Ren&#233; Winkler
Ren&#233; Winkler

Reputation: 7058

Each catch block is an exception handler that handles the type of exception indicated by its argument. Once the exception is handled, it will not be thrown anymore unless you rethrow the exception in the catch block.

Upvotes: 0

Duncan Jones
Duncan Jones

Reputation: 69339

If I understand you correctly, you are asking:

void someMethod() throws SomeException {    
  try {
    doSomethingElse()
  } 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.

Upvotes: 3

Related Questions