0

In Exception hierarchy, RuntimeException is child class of Exception is unchecked but if we use Exception then it is treated as checked Exception why?

  • http://stackoverflow.com/questions/6115896/java-checked-vs-unchecked-exception-explanation maybe? –  May 19 '16 at 10:13
  • 1
    Because that's how it was decided and designed. – Kayaman May 19 '16 at 10:14
  • 1
    Because that is the way they are defined in [JLS chapter 11](https://docs.oracle.com/javase/specs/jls/se8/html/jls-11.html). – Andy Turner May 19 '16 at 10:15
  • 1
    Think of it as "being unchecked is inherited" instead of "being checked is inherited", and it will make sense. – mastov May 19 '16 at 10:16
  • 1
    @Kayaman: or that's how checked exceptions were kludged into Java ;-) – Bathsheba May 19 '16 at 10:16
  • @Bathsheba There were many design crimes committed back in the day... :) – Kayaman May 19 '16 at 10:23
  • thanks for your response. But how is it possible in case I want to create my custom exceptions in the same manner. If my parent custom Exception is extending Exception then how my child Exception can extend this custom parent Exception (Checked) and be UncheckedException as multiple Inheritance is not supported. –  May 19 '16 at 10:23
  • @JTalreja: If it was designed the way you seem to expect it (according to your original question), you would have the same problem. You can't make one decision (checked or unchecked) in the parent class and change it for subclasses. You always need 2 hierarchies. – mastov May 19 '16 at 10:31

1 Answers1

0

From the Oracle Java documentation:

Runtime exceptions can occur anywhere in a program, and in a typical one they can be very numerous. Having to add runtime exceptions in every method declaration would reduce a program's clarity. Thus, the compiler does not require that you catch or specify runtime exceptions (although you can).

So they added RuntimeExceptions to deal with common run-time problems, without the hassle of catching-or-throwing every single time.

Diego Freniche
  • 5,225
  • 3
  • 32
  • 45