Why does java.lang.RuntimeException
(or sub-class of it) not force us to write code in try/catch
Provided:
java.lang.Exception
or sub-class of it (termed as checked-exception) forces
us to write code in try/catch
block or forces us to handle it.
Then why does java.lang.RuntimeException
or sub-class (termed as unchecked-exception) not force us to write code in try/catch block even if it is extending java.lang.Exception
Added Example:
public class ExceptionTest
{
public static void main(String[] args)
{
// HOW COMPILER COME TO KNOW or DECIDE THIS METHOD IS THROWING RuntimeException or Exception (Keep in mind java.lang.RuntimeException is again extending java.lang.Exception)
new ExceptionTest().test_1();
}
public void test_1() throws MyException
{
}
}
class MyRuntimeRuntimeException extends RuntimeException
{
public MyRuntimeRuntimeException() {
// TODO Auto-generated constructor stub
}
}
class MyException extends Exception
{
public MyException() {
// TODO Auto-generated constructor stub
}
}