M
Michal Kleczek
Lew said:Hence the 'catch' block. What resource can leak there?
A file handle. You do not call close() on FileReader in case BufferedReader
constructor throws.
Lew said:Hence the 'catch' block. What resource can leak there?
A file handle. You do not call close() on FileReader in case BufferedReader
constructor throws.
Lew said:Non-issue. If FileReader doesn't then BufferedReader won't.
Lew said:Non-issue. If FileReader doesn't then BufferedReader won't.A file handle. You do not call close() on FileReader in case BufferedReaderLew said:Michal Kleczek wrote:
public void useResource( String src, String dst ) {
final BufferedReader reader;
try {
reader = new BufferedReader( new FileReader( src ));
} catch ( IOException exc ) {
[...]
is not exception safe - if BufferedReader constructor throws you've got a
resource leak.
Hence the 'catch' block. What resource can leak there?
constructor throws.
Michal said:1. It can always throw a RuntimeException or an Error. Depending on
situation it should or should not be handled gracefully hence it _can_ be an
issue. If it is a library (reusable) code it certainly is.
2. My original reply to Robert's post was:
<quote>
That only works because both InputStreamReader and BufferedReader
constructors don't throw.
But it is not safe to do the same for:
final ObjectInputStream ois =
new ObjectInputStream(new FileInputStream("foo.txt"));
</quote>
You've read it before replying, haven't you?
ok, got it: BufferedReader doesn't throw, making it a non-issue forAndreas Leitgeb said:How is a leaked(for uncontrollable time) file handle a non-issue?Lew said:Non-issue. If FileReader doesn't then BufferedReader won't.Lew wrote:
Michal Kleczek wrote:
public void useResource( String src, String dst ) {
final BufferedReader reader;
try {
reader = new BufferedReader( new FileReader( src ));
} catch ( IOException exc ) {
[...]
is not exception safe - if BufferedReader constructor throws you've got a
resource leak.
Hence the 'catch' block. What resource can leak there?
A file handle. You do not call close() on FileReader in case BufferedReader
constructor throws.
It was unclear, because the actual verb was missing. Obviously nowThe other statement is unclear, ...
Lew said:If it throws an Error your resources are released because the program
crashes.
Same for RuntimeException.
and you were mistaken then, too.
I know of a lot of cases when the program will not (and should not) crash
after RuntimeException (or even an Error).
Are you suggesting to write non-robust code leading to potential resource
leaks?
No.
It might:
int erronousSize = -2048;
BufferedReader reader = new BufferedReader( new FileReader( "test.txt" ), erronousSize );
Anyway, surely the particular behaviour of BufferedReader is
tangential to the discussion? It's the problems of the general
pattern that's of real interest.
Lew said:If FileInputStream doesn't have an I/O error, then ObjectInputStream
won't,
either. The I/O condition will have been handled by the time the OIS is
constructed.
Michal said:ObjectInputStream constructor can also throw IOException (actually its
subclass: StreamCorruptedException) if the underlying InputStream does not
start with a valid header.
Lew said:Precisely. If these issues that Michal Kleczek is so worried about are
actually a problem, just extend the pattern. Is that so difficult to
infer,
Michal, really? Just because you think an example is incomplete doesn't
mean the point isn't valid, right, Michal?
Michal said:Oh, sure.
But it is so much fun to point out (potential) issues in _other_ people's
code
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.