91. Checked vs Unchecked Exceptions.
Table of Contents
In Java, Exceptions are of two types - checked and unchecked.
Checked Exceptions
-
These are checked at the compile time.
-
If some code within a method throws a checked exception, we must do either of the following:
a. Put the throws XYZException in the method definition.
b. Put a try-catch block within the method (not where it is being called)
-
Eg. are IOException or Compile Time Exception
Unchecked Exception
- These are the exceptions that are not checked at compile time. It is up to the programmers to be civilized and specify or catch the exceptions.
- In Java, exceptions under Error and RuntimeException classes are unchecked exceptions, everything else under throwable is checked.
- We don't require a throws clause or explicit exception handling (try-catch).
- Eg. NPE, ArithmeticException (divide by 0), ArrayIndexOutOfBoundsException.
Should we actually catch Runtime Exceptions? Well that's a debate! I encourage the reader to do their own DD on it.