I played with some programs with exceptions and came across with the fact that I was surprised that I had never met before.
import java.io.*;
class ExceptionTest
{
public static void main(String[] args)
{
try
{
System.out.println("hello");
}
catch(IOException ioe)
{
System.err.println("ioexception was thrown!!");
}
}
}
a compiler error occurs:
ExceptionTest.java:10: exception java.io.IOException is never thrown in body of
corresponding try statement
catch(IOException ioe)
^
1 error
My question is: why is this so?
Why is the compiler worried about a piece of code, it is sure that it will never reach. It may just be a warning, not a complete mistake. In the end, we might want to keep the catch block as a backup for some future changes that we could make, or maybe the programmer just forgot to delete it after some changes.
I know that I know that this saves us from unnecessary keystrokes, etc. But this should not interfere with the compiler to cause an error. Or is there some other reason for this?
, catch IOException , catch ... ?
Thnx !