java check and uncheck code example

Example 1: Unchecked exception

All subclasses of RuntimeException are called unchecked exceptions. 
These are unchecked exceptions because compiler does not checks if a method 
handles or throws exceptions. Program compiles even if we do not catch the 
exception or throws the exception. If an exception occurs in the program,
program terminates. It is difficult to handle these exceptions
because there may be many places causing exceptions.
Example : 
1) Arithmetic Exception
2) ArrayIndexOutOfBoundsException
3) ClassCastException
4) IndexOutOfBoundException
5) NullPointerException
6) NumberFormatException
7) StringIndexOutOfBounds
8) UnsupportedOperationException

Example 2: Can we use catch statement for checked exceptions

If there is no chance of raising an exception in our code then we can’t 
declare catch block for handling checked exceptions. This raises compile time 
error if we try to handle checked exceptions when there is no possibility 
of causing exception.

Tags:

Misc Example