«« Next » « Previous
«« Next » « Previous

Link Details

Link 68877 thumbnail
User 273400 avatar

By sharvil
via sharvil.nanavati.net
Submitted: Mar 02 2008 / 15:16

When I release a resource, I really don't want to hear back from it. Java's standard library doesn't follow this rather simple principle and can result in some subtle bugs. If you design reusable code, take a look and make sure you don't fall into the same trap.
  • 2
  • 1
  • 227
  • 31

Comments

Add your comment
User 241974 avatar

dkharlamov replied ago:

0 votes Vote down Vote up Reply

Yes and no. Checked exception is a good way to let programmer know about any issues with certain resource. On the other hand you did find good example to be improved on Java side. But I would still have those checked exceptions as you may have "connected" resources (which should be closed one after another) or so.

User 273400 avatar

sharvil replied ago:

0 votes Vote down Vote up Reply

I agree that checked exceptions are a great way to notify the caller of problems with the resource. However, I don't think it's reasonable to force the caller to take action (via checked or unchecked exception) when the caller has declared that it has no more interest in the resource. For methods that acquire and interact with resources, I agree with you wholeheartedly.

User 236137 avatar

dzonelurker replied ago:

0 votes Vote down Vote up Reply

Warning: defective code!

User 273400 avatar

sharvil replied ago:

0 votes Vote down Vote up Reply

Hey there, dzonelurker. Could you please elaborate on your comment? The first snippet is intentionally defective and I explain why that sort of code should be avoided. I'd be more than happy to revise the second snippet but I can't spot any problem with it (unless 'close' throws a RuntimeException which is not the point of the illustration and would just increase clutter without adding value).

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.

Voters For This Link (2)



Voters Against This Link (1)