0

What is the difference between an IOException class that extends the Exception class and RuntimeException class that also extends Exception class?

Consider this code:

public class IOExceptionvsRuntime {
    public static void main(String[] args) {
        try {
            throw new IOException();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        throw new RuntimeException();
    }
}

Although both these classes extend the Exception class and yet the IOException has to be handled whereas compiler does not enforce handling of RuntimeException. I know the conceptual difference between the two that RuntimeException is an unchecked exception whereas IOException is a checked one.

It seems like there is some compiler-level handling, and I was interested in knowing more about how the compiler handles these.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • 1
    This is quite a vague question - the compiler enforces certain behaviour for checked exceptions, just like it does for many other aspects of the language/type system. – Oliver Charlesworth Aug 24 '16 at 09:39
  • @OliverCharlesworth: True that the compiler enforces certain behaviour for checked exceptions. But since both are extending exception class, I was curious on how the compiler distinguishing between these two. Such as type-checking (instance of RuntimeException). Was more interested in the programatic logic that compiler uses. Thanks! – action0801 Aug 24 '16 at 10:24

0 Answers0