Skip to main content

java.io.Closeable: Too late for a change?

5 replies [Last post]
mcnepp
Offline
Joined: 2005-06-22

I wonder whether it would be feasible to change the declaration of the interface java.io.Closeable (introduced in java 1.5) as follows:
(Current)

<br />
public void close() throws IOException;<br />

(Proposed)
<br />
public void close() throws Exception;<br />

The rationale behind this change is that there are many resource-related classes that would benefit from implementing Closeable, but cannot do so because their close methods do not throw IOException. (Prominent example: java.sql.Connection).
Please note that the change would not introduce incompatiblities with regards to the implementing classes or extending interfaces: they would still restrict their throws clause to a more specific Exception type.
The change would, however, cause a source incompatiblity with code actually using the interface java.io.Closeable and not catching java.lang.Exception. Such code would have to be refactored in order to be compilable. (Libraries using java.io.Closeable would not have to be re-compiled, though!)
What do you think?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
s690716
Offline
Joined: 2004-03-04

My five cent...

A nice add-on would be a Disposable interface like IDisposable in .NET to free any kind of resources (like some AWT/Swing components, JDBC connections, JNDI resources etc).

void dispose();

The "using" directive from C# as a shortcut to try-finally in combination with a Disposable interface is a good example, how to manage resources in a unified and simple way for user - instead of missing destructors or abuse finalize

mcnepp
Offline
Joined: 2005-06-22

> My five cent...
>
> A nice add-on would be a Disposable interface like
> IDisposable in .NET to free any kind of resources
> (like some AWT/Swing components, JDBC connections,
> JNDI resources etc).
>
> void dispose();
>
> The "using" directive from C# as a shortcut to
> try-finally in combination with a Disposable
> interface is a good example, how to manage resources
> in a unified and simple way for user - instead of
> missing destructors or abuse finalize

Right you are! That would of course be the most elegant solution to the Resource Aquisition/Disposition problem.
Unfortunately, you can't bring this up in Java discussions: people inevitably start lecturing about the Garbage Collector (even though the "using"-proposal does not at all interfere with it).

sjasja
Offline
Joined: 2004-08-15

I did a bit of googling on IDisposable. Did I understand right: these bits of code accomplish the same thing?
[code]
// C#

using (foo = getFoo()) {
...code...;
...code...;
...code...;
}

// Java

try {
foo = getFoo();
...code...;
...code...;
...code...;
} finally {
Closer.close(foo);
}
[/code]
Is there more beef to IDisposable's elegance than less typing?

There are a couple of threads here that think about alternate syntax for try/finally. I wouldn't mind terribly seeing the declaration, creation, and scheduling-for-destruction being closer together. On the other hand I haven't exactly worn my fingers to the bone typing try/finally so far. Typing all the try/finally blocks in a 1000-man-hour project takes, what, 10 minutes...

tackline
Offline
Joined: 2003-06-19

Using methods that throw Exception is not nice. And yes, you can't reakky go around adding exceptions to interfaces that are already in use.

Better would be something like

public interface CloseableResource {
void close() throws EXC;
}
public interface Closeable extends CloseableResource {
}

rjlorimer
Offline
Joined: 2003-06-10

Tackline,

I like your suggestion - good use of generics and interface inheritence.

Regards,

R.J.