• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Troubleshooting Java: final, finally, finalize

 
Ranch Hand
Posts: 39
Java ME Quarkus Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi troubleshooters,


Typical interview question is "Can you explain the difference between final, finally, and finalize in Java?". And it is pretty straightforward:
final = method is executed only when we call it
finally = block is executed as soon as the try-catch block is executed
finalize = method is executed just before the object is destroyed

But what if you are asked to describe some best practices for finally and catch blocks in exception handling (or how to handle multiple exceptions in a single try-catch block)? Do you have some real-life example?


Regards
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not convinced about that answer, I am afraid.
You should not use one of the three.
I disagree with the other two, I am afraid.
Why do you think there is a difference in the usage of finally when catching several exception types and only one exception type?
 
Marshal
Posts: 4510
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I listened to an interesting podcast from Oracle recently where they discussed finalization: Inside Java Episode 21 “JEP 421 and Finalization Deprecation”
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was worth hearing. Finalisation was a feature inherited from C++ which wasn't actually necessary and turns out to be more trouble than it's worth.
SY: Are you clear on the difference between those three features yet?
 
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That was worth hearing. Finalisation was a feature inherited from C++ which wasn't actually necessary and turns out to be more trouble than it's worth.


While it's not generally necessary for user code to perform finalization, important classes provided by the JDK do heavily rely on it. Besides, while uncommon, it's not unheard of for user code to want to perform cleanup when an object is garbage collected. That's why Java 9 gave us the Cleaner class.

So while I agree that finalization isn't generally needed in user code, whether it's unnecessary in all user code depends on whether you include the use of tools like the Cleaner class under the term "finalization".
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I take your point; it would have been netter to say, “finalisers.”

[/tangent]SY: have you worked out what the difference is between the three similar‑sounding words?
 
Stepankha Yuliannia
Ranch Hand
Posts: 39
Java ME Quarkus Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Campbell Ritchie
Well, I think the finally block is still useful for containing cleanup code that needs to be executed regardless of whether the exception was caught or not...

When catching several exception types in a try block, it is possible that different exceptions may be thrown and caught at different points within the block. In this case, the finally block ensures that any code that needs to be executed after the try block, regardless of the type of exception caught, will be executed. For example, if resources were allocated in the try block, they can be cleaned up in the finally block to ensure that they are always released. I see your point though - catching only one exception type in a try block, is unlikely that different exceptions will be thrown and caught at different points within the block.

@Ron McLeod
Interesting read, thanks: JEP 421: Deprecate Finalization for Removal

 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.

Stepankha Yuliannia wrote:. . . the finally block is still useful for containing cleanup code that needs to be executed regardless of whether the exception was caught or not. . . .

What about try‑with‑resources? I am still not quite sure you have understood finally.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stepankha Yuliannia wrote:Typical interview question is "Can you explain the difference between final, finally, and finalize in Java?".



If this is a typical interview question then I'm glad I don't have to go to interviews. It's no more sensible than "Can you explain the difference between a dog, the theory of relativity, and Barcelona?"

But what if you are asked to describe some best practices for finally and catch blocks in exception handling (or how to handle multiple exceptions in a single try-catch block)? Do you have some real-life example?



A much better question.
 
Ranch Hand
Posts: 122
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stepankha Yuliannia wrote:Hi troubleshooters,


Typical interview question is "Can you explain the difference between final, finally, and finalize in Java?". And it is pretty straightforward:
final = method is executed only when we call it



isn't that true for most of the methods? the final here signifies entirely for a different purpose
 
reply
    Bookmark Topic Watch Topic
  • New Topic