Java unreported exception?

What you're referring to are checked exceptions meaning they must be declared or handled. The standard construct for dealing with files in Java looks something like this.

What you're referring to are checked exceptions, meaning they must be declared or handled. The standard construct for dealing with files in Java looks something like this: InputStream in = null; try { in = new InputStream(...); // do stuff } catch (IOException e) { // do whatever } finally { if (in! = null) { try { in.close(); } catch (Exception e) { } } } Is it ugly?

Sure.Is it verbose? Sure. Java 7 will make it a little better with ARM blocks but until then you're stuck with the above.

You can also let the caller handle exceptions: public void doStuff() throws IOException { InputStream in = new InputStream(...); // do stuff in.close(); } although even then the close() should probably be wrapped in a finally block. But the above function declaration says that this method can throw an IOException. Since that's a checked exception the caller of this function will need to catch it (or declare it so its caller can deal with it and so on).

Handled" I suppose means try/catch. And what is "declared" in this case? – Sejanus Jan 19 '10 at 6:41 Declared means added to the method signature in a "throws" clause.

– Stephen C Jan 19 '10 at 7:03 Got it now, thanks – Sejanus Jan 19 '10 at 7:29.

1 for JLS, but more recently: java.sun. Com/docs/books/jls/third_edition/html/exceptions. Html – trashgod Jan 19 '10 at 15:30.

Java's checked exceptions make programmers address issues like this. (That's a good thing in my opinion, even if sweeping bugs under the carpet is easier. ) You should take some appropriate action if a failure occurs.

Typically the handling should be at a different layer from where the exception was thrown. Resource should be handled correctly, which takes the form: acquire(); try { use(); } finally { release(); } Never put the acquire() within the try block. Never put anything between the acquire() and try (other than a simple assign).

Do not attempt to release multiple resources in a single finally block. So, we have two different issues. Unfortunately the Java syntax mixes up the two.

The correct way to write such code is: try { final FileOutputStream rawOut = new FileOutputStream(file); try { OutputStream out = new BufferedOutputStream(rawOut); ... out.flush(); } finally { rawOut.close(); } } catch (FileNotFoundException exc) { ...do something not being able to create file... } catch (IOException exc) { ...handle create file but borked - oops... }.

Thanks, I always appreciate good coding practice advices. – Sejanus Jan 20 '10 at 9:56.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions