Please tell me more about constructors that use a try-finally block in them?

What is a use case for this being in a constructor? What is the problem with using “finally” to handle exceptions in constructors?
9 Replies
JavaBot
JavaBot2w ago
This post has been reserved for your question.
Hey @SidKid! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st2w ago
You might want to use a try with resources or similar to load a file in the constructor or similar
SidKid
SidKidOP2w ago
is try with resource just the same thing as try catch and try finally?
dan1st
dan1st2w ago
it's basically a better (safer, easier to use) way of doing try finally with stuff that's AutoCloseable
SidKid
SidKidOP2w ago
oh so try-resources is its own concept? i thought a generic trerm for try-finally and try-catch
JavaBot
JavaBot2w ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Cello Ski
Cello Ski5d ago
Try_resources is used usually when there are resources that need to be closed after an exception has been encountered or allow other part of the program to access the same resources.
JavaBot
JavaBot5d ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?