1

Can a Catch block Follows Finally block in exception?

finally{ baz(); }catch(MyException e){} 
5
  • 3
    There's something very final about finally. It really suggests that there's nothing else after it. :) Commented Jul 10, 2013 at 4:37
  • Ten seconds with your compiler or three minutes in the language specification would answer this. -1 Commented Jul 10, 2013 at 4:53
  • +1 because it really isn't that bad a question... Commented Jul 10, 2013 at 4:56
  • @pstanton: It would be a much better question if he explained his rationale behind the question. Again, I smell an XY Problem lurking here. Commented Jul 10, 2013 at 12:30
  • @HovercraftFullOfEels true re XY, however i still found the question/answers insightful even though i know the language reasonably well ... Commented Jul 10, 2013 at 13:53

5 Answers 5

6

No It can't . A try should be followed by a catch or finally . If there is a catch then finally is the last block. This order will again depend on nesting . So you can have a nested structure like below , but again try is followed by a finally or catch . The catch after the internal finally block belongs to the outer try.

try { // outer try try { // inner try } finally { } } catch(SomeException e) { } 

You can read more about it in JLS 14.20.

Sign up to request clarification or add additional context in comments.

Comments

2

Can a Catch block Follows Finally block in exception?

No, not if it's for the same try.
But you could find this out quickly by trying it with your trusty Java compiler.

Note, that having said this, you can nest try/catch blocks if need be, but again the catch block after the finally would be on a different scope level than the preceding finally.

I'm curious as to the instigation for this question. A finally() really must always be final, and always be called, else it is not a true finally. What are you trying to accomplish with this? Because I'll bet that there is a better way, that perhaps what you have is an XY problem, and that the solution is to try a completely different approach. Or is this a homework question?

Comments

1

Only for the following case it is possible

try{ try{ } finally{ } baz(); }catch(MyException e){} 

But it is a totally different strucute. With the structure of your question it is not possible.

Comments

1

The construct is called as try-catch-finally. Where try need to be followed by either catch or finally or both. If all the three are present, the finally block always executes when the try block exits no matter whether an exception has occured or not.

The reason why you are looking for a catch after finally is probably how to handle an exception in finally. There is no other way rather than putting another try/catch/finally block inside that :-(

Comments

1

You can check that by your own. Following scenarios are possible with try-catch-finally

case 1

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

case 2

 try{ }finally { } 

case 3

 try { }catch (Exception e){ } 

As you can see finally should come after try or catch. similarly finally can't be before catch.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.