Java's AutoCloseable
feature "reuses" try/catch semantics:
try (Stream x = ...) {
// work with x
}
// x is out of scope and was auto-closed
I'm curious why they didn't introduce new semantics for this new feature. try
implies that you expect the body to throw an exception at one point or another (or that it may). This seems much different to me than "instantiate this resource and close it when I'm done". That doesn't have anything to do with handling exceptions. Why not something like this...
with (Stream x = ...) { ... }
using (Stream x = ...) { ... } // I know, C# syntax
I'm not looking to fuel a debate, I want to know the reason the Java team decided to re-use try
for this feature.