-2

Here test is not throwing an Exception object , yet i had handled it . Since Exception is an checked exception shouldn't it throw a compiler error of unreachable code in the catch block

class Ece extends Exception {}
public class Excep {
    public static void test()  { }
    public static void main(String[] args) {
        try {
            test();
        } catch (Exception E) {

        }
    }
}
Yash Singhal
  • 357
  • 1
  • 2
  • 8
  • `Exception` is the parent of **both** checked and unchecked exceptions. So you are not prevented from catching `Exception` in code that doesn't declare checked exceptions (clearly because runtime exceptions may be raised) – ernest_k Sep 02 '18 at 10:42
  • Because `test()` might throw any unchecked exception also, which would be caught by the `catch` block. – Fullstack Guy Sep 02 '18 at 10:43

1 Answers1

1

The class Exception has RuntimeException as subclass. RuntimeException and its subclasses do not need to be declared in methd signature.

In this case you are catching all possible subclasses of Exception, including all that subclasses that do not need signature declaration. If your test method throws for example ArrayIndexOutOfBoundsException you will be able to catch and handle it, yet test signature will not be affected.

Further reading here

Lorelorelore
  • 3,335
  • 8
  • 29
  • 40