Hacker News new | ask | show | jobs
by kjetil 5516 days ago
Java finally (pun intended) gets a similar feature in Java 7 later this year: try-with-resources:

http://download.java.net/jdk7/docs/technotes/guides/language...

1 comments

It's unclear to me why he says that the JDK7 addition doesn't really solve the problem. It seems to me that it is exactly this problem that ARM blocks are trying to solve. It's still not as clean as leveraging deconstructors when an object goes out of scope but it's much better than before.
Well, it's an awkward solution, because exceptions have this general problem of blowing away everything you're doing, and it's only a solution for this one case.

The solution to this problem that makes sense to me is either conditions and restarts a la Common Lisp, or a type system that can handle multiple return values of different types in a sane way (e.g. return either a result or an error code and then pattern match against them) so that you don't always need to throw an exception in order to deal with an error.

Exceptions are little more than a formalization of a particular pattern of what Haskell might call the Either monad for return values, combined with pattern matching on the error type, and automatic unwinding. A condition system has positive value; but multiple return types with error codes alongside, like Go, are a regression from exceptions, IMO.