0
0

More than 1 year has passed since last update.

unchecked exception

Last updated at Posted at 2023-01-25

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.
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0