NullPointerExceptionのようなRuntimeException系のexceptionは
uncheckedExceptionで、try,catch不要
public class Outer {
public static void main(String[] args) {
NullPointerException npe = new NullPointerException();
System.out.println(npe instanceof RuntimeException);
String[] str = null;
String s = str[0];
}
}
Exception in thread "main" true
java.lang.NullPointerException
at com.mycompany.mavenproject1.Outer.main(Outer.java:13)
InterruptedExceptionは、RuntimeExceptionではない。
public static void main(String[] args) {
Exception ie = new InterruptedException();
System.out.println(ie instanceof RuntimeException);
//Thread.sleep(1000);
}
false
try,catchなしのため、compile error
public class Outer {
public static void main(String[] args) {
Exception ie = new InterruptedException();
System.out.println(ie instanceof RuntimeException);
Thread.sleep(1000);
}
}
Exception in thread "main" java.lang.RuntimeException: Uncompilable code - 例外java.lang.InterruptedExceptionは報告されません。スローするには、捕捉または宣言する必要があります
at com.mycompany.mavenproject1.Outer.main(Outer.java:1)
Command execution failed.