-1

Why unchecked exceptions are propagated while checked exceptions are not propagated in Java? I googled it but didn't find any satisfactory explanation.

Shubhaw Kumar
  • 691
  • 4
  • 16
  • 5
    Checked and unchecked exceptions are both propagated in the same way. The only difference between them is whether you have to declare them. Can you clarify your question? – T.J. Crowder Jul 12 '16 at 08:31
  • checked exceptions need to be specified using throws clause while unchecked exceptions are automatically propagated. why? – Shubhaw Kumar Jul 12 '16 at 08:35
  • That's what makes the difference between the two. As to why there is that distinction there have been questions on SO before and it is addressed on various websites, too. See http://stackoverflow.com/q/6115896/982149 – Fildor Jul 12 '16 at 08:41

1 Answers1

1

checked exceptions need to be specified using throws clause while unchecked exceptions are automatically propagated. why?

Because that's how they're designed. The idea was to have a class of exceptions (checked exceptions) a method can declare that the calling code is required to allow for (by explicitly handling or explicitly declaring), and another class of exceptions (unchecked exceptions) that the calling code doesn't have to explicitly allow for. The latter were for things like "out of memory" and such (things the code wouldn't be able to do anything about anyway) and things like NullPointerException (things that are programming errors that should be caught in test; just about every method on the planet would need to declare it if it were a checked exception).

How well that worked out depends on who you ask, but that was the idea.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875