Hi all, I quoted the code here for convenience
public class AQuestion
{
public static void main(String args[])
{
System.out.println("Before Try");
try
{
}
catch(java.io.IOException t)
{
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
}
I've tyied the code and finded that the answer is 1 and that the reason is not so simple as i assumed.
I can't find a rule in the specification says that the exception in the catch clause necessarily can ever be thrown in the try clause, is there any?
I looked up the specification and find there's a seemingly-related reason, pls. look at the following quote from the spec.
A catch block C is reachable iff(if and only if) both of the following are true:
Some expression or throw statement in the try block is reachable and can throw an exception whose type is assignable to the parameter of the catch clause C. (An expression is considered reachable iff the innermost statement containing it is reachable.)
There is no earlier catch block A in the try statement such that the type of C's parameter is the same as or a subclass of the type of A's parameter.
It seems that the statement in the catch clause is considered unreachable by the compiler so it complained, but I find if I replace the "java.lang.IOException" with "ArithmeticException", the compiler doesn't object to the code any more, why? just because "ArithmeticException" is the subclass of "RuntimeException"? Does it mean that unreachable statement could be acceptable in some special case or that an empty block(i.e {} ) is considered a potential RuntimeException thrower?
I really need a clarification, thanks in advance.