0

In the program:

class Ideone
{
    public static void main (String[] args){
        try{} catch (NumberFormatException e){ }
    }
}

DEMO

Actually, the JLS 11.2.3 describes behavior in such cases:

It is a compile-time error if a catch clause can catch checked exception class E1 and it is not the case that the try block corresponding to the catch clause can throw a checked exception class that is a subclass or superclass of E1, unless E1 is Exception or a superclass of Exception.

In my case the catch clause can catch NumberFormatException that is neither Exception nor superclass of Exception. Try block can throw nothing, because there are no statements in there. So, why the code is compiled fine?

St.Antario
  • 26,175
  • 41
  • 130
  • 318
  • Checked and unchecked exceptions are explained in detail on the official Java SE tutorial: http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html – gerrytan Feb 15 '15 at 10:57

1 Answers1

3

It is a compile-time error if a catch clause can catch checked exception...

NumberFormatException is not a checked exception. It's a sub-class of IllegalArgumentException, which is a sub-class of RuntimeException. Therefore the entire clause you quoted from the JLS does not apply.

Replace NumberFormatException with some exception that is not a sub-class of RuntimeException (for example, IOException), and you'll get a compile-time error.

Eran
  • 387,369
  • 54
  • 702
  • 768