-4

In Java, why does the "finally" block exist:

try {
    ...
} catch(...) {
    ...
} finally {
    // instructions lastly executed when the "try" block is quit
}

instead of a "firstly" block ?

try {
    ...
} firstly {
    // instructions firstly executed when the "try" block is quit
} catch(...) {
    ...
}
Shiro
  • 2,610
  • 2
  • 20
  • 36
Codoscope
  • 892
  • 1
  • 10
  • 18

2 Answers2

1

This would be the same as

try {
    try {
        ...
    } finally {
        // instructions firstly executed when the outer "try" block is quit
    }
} catch(...) {
    ...
}

This isn't a common pattern, but if it was, it could be supported in a more idiomatic way. Generally speaking finally is used to close of any resources which you no longer need and you are unlikely to want prevent access to those resources in your exception handling.

The only difference is that the firstly block is executed before the catch clauses, instead of after as the finally block does. So for instance, it can properly close the resources, before the catch blocks (which could do a System.exit...).

I assume the closing of resources made more sense to place at the end of the block rather than in the middle.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    You get a point. However, local variables in the try block cannot be used in the catch. If they are declared outside, they could be null (because an exception prevented them from being initialized before the catch). Moreover, the resource could launch an exception with the attempt to create one, and it couldn't be used in the catch. Btw: the finally would be the same as: `try { try {...} catch... } firstly {...}`. – Codoscope Jun 26 '16 at 11:27
  • @Qu'est-cet'yont I think you have a point and +1'ed your question, but I think it's not as logical to arrange this way, though it could be because I am used the way it is. Possibly it works this way in some language they copied the feature from. – Peter Lawrey Jun 26 '16 at 11:30
  • Thank you. I put a +1 to your answer because it brings some pieces of answer (although it doesn't solve it completely). I don't understand why I get negative points on this question. – Codoscope Jun 26 '16 at 12:55
  • @Qu'est-cet'yont Why-are-things-the-way-they are questions often don't have a definitive answer and tend to get downvoted on SO. – Peter Lawrey Jun 26 '16 at 20:28
0

In Java, why does the "finally" block exists:

A finally block exists so that it is always executed no matter where the Exception occurs ( either in try or in catch or doesn't occur at all ). finally is used to put code like closing file streams, clean-up code and the like...

If you don't like finally, then there is try with resources. You might want to use that.

In Java, why does the "finally" block exists, instead of a "firstly" block ?

As the name suggests finally must contain the cleanup code, it cannot be placed before catch and be called some fancy firstly. The code in finally is executed even if an Exception is raised in catch block.

John Strood
  • 1,859
  • 3
  • 26
  • 39
  • I'm afraid that's not the point. The firstly block is executed even if an exception is raised in a catch clause. The firstly block can contain the cleanup code as well as the finally one. – Codoscope Jun 26 '16 at 11:09
  • @Qu'est-cet'yont Then how is `finally` different from `firstly` at all? – John Strood Jun 26 '16 at 11:11
  • The only difference is that the firstly block is executed before the catch clauses, instead of after as the finally block does. So for instance, it can properly close the resources, before the catch blocks (which could do a System.exit...). – Codoscope Jun 26 '16 at 11:15
  • @Qu'est-cet'yont Unfortunately, that's not what you do. You always handle an exception then, close your resources, not the other way around. – John Strood Jun 26 '16 at 11:24