Skip to main content

effective ways to handle exceptions with streams?

No replies
Joined: 2009-07-30

I'd like to know how to handle exceptions effectively when using streams. I mean I want to know best practices when writing try-catch-finally block for streams.

Some streams might throw exceptions when they're created, read, written or closed. We often open both input and output streams. It means there're a number of ways the exceptions might be thrown.

Do you catch or throw exceptions for all cases? Do you prefer two or three-depth nested try-catch-finally block?

I'd like to know the source template to handle exceptions with streams. If I suppose both input/output socket streams. It will throw exceptions in many cases. What's the best way or your favorite way?

another question is ... which way do you prefer when chaining streams?

FileInputStream fileIn = new FileInputStream("sample.dat");
BufferedInputStream bufferedIn = new BufferedInputStream(fileIn);
DataInputStrem dataIn = new DataInputStream(bufferedIn);


DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("sample.txt")));