为什么java.lang.RuntimeException
(或它的子类)不强迫我们在 try/catch 中编写代码
假如:
java.lang.Exception
或它的子类(称为检查异常)迫使我们在try/catch
块中编写代码或强制我们处理它。
那么为什么java.lang.RuntimeException
or 子类(称为 unchecked-exception)不强迫我们在 try/catch 块中编写代码,即使它正在扩展java.lang.Exception
添加示例:
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
}
}