开发者

eclipse asks me to surround with try/catch in finally block - possible to disable it?

In one of my Java application's code, I have a try-catch-finally block in which the try block creates some input and output streams and if something goes wrong I close any earlier opened streams in finally.

finally
{
    if(inputStream != null)
        inputStream.close();
    if(outputStream != null)
        outputStream.close();
}

But the <stream>.close() line in Eclipse shows error that "Unhandled exception IOException" in the code for this line and shows that the solution is to include another try/catch in the finally block which would seem to be bad as a programming practice and I don't want in finally block.

My question is: is it possible to remove this error in Eclipse and use try/catch only when I need it instead of eclipse telling me to do the try/catch add. (Since I am already trying to avoid exception by replacing try/catch with if/else as pos开发者_开发百科sible).


This is not an Eclipse error, it is a Java compiler error. Eclipse is merely reporting the Java compilation error for you. There is no way to "turn it off" as the code does not compile without the try/catch clause. It is a safety feature in Java that forces you to handle commonly thrown Exceptions.

Methods have Exceptions in their signature. For example, InputStream.close() throws an IOException, forcing you to handle it in a try/catch block.

public void close() throws IOException {
    ...

Throwing an Exception is a way of telling the program that a significant problem - that must be handled - has occurred.

My question is: is it possible to remove this error in eclipse and use try/catch when I need it otherwise not instead of eclipse telling me to do try/catch add.

No, it is not possible.

(Since I am already trying to avoid exception by replacing try/catch with if/else as possible).

You should generally never try to replace try/catch blocks with if/else blocks. They are two distinct features with distinct purposes.

Exceptions are an essential Java feature. Read about it and understand it.


Properly this should be done something like this to ensure that we attempt to close both streams.

finally
{
   try {
     if(inputStream != null)
        inputStream.close();
   }
   catch(Exception e)
   { /* Ignore */ }

   try {
     if(outputStream != null)
        outputStream.close();
   }
   catch(Exception e)
   { /* Ignore */ }
}


If you don't care about handling the exception, check out the Apache commons io library.

org.apache.commons.io.IOUtils.closeQuietly(inputstream)

Works for outputstream, writer and reader too.


IOException is not something that you can avoid because it might happen because of circumstances outside your control. (Broken network connection, hard drive error etc). Eclipse is totally right that inputStream.close() and outputStream.close() itself may throw exception, and you must be prepared to handle that somehow. There is no way to silence this error because it is not Eclipse's pickiness; your code is not valid Java code as it stands.

You may declare that your function throws IOException and delegate the handling of the exception to the caller, or you must bite the bullet and handle (and probably ignore) the IOException yourself. I think there is a utility function in the IOUtils library of Apache Commons that encapsulates this logic, which would make your code cleaner (and hide the fact that you are silently ignoring an IOException).


But the .close() line in eclipse shows error that Unhandled exception IOException in code for this line and shows solution to include another try/catch in finally block which is bad as programming practice and i don't want in finally block.

The fact that close methods can also throw IOExceptions, so that you have to have nested try/catch clauses inside the finally block, is an unfortunate situation.

But there is really nothing much you can do about this. You need those (ugly) finally blocks to handle exceptions properly.


There is nothing wrong with eclipse.It just show you have a compile error in your code.you cannot replace try-catch with if-esle and here it required try catch.You need this try catch not because the input stream may be null as you try in if else.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜