🕌
Javaのエラーいくつか
以下のコードで起こるエラーをいくつか解説していきます。
3 / 0
String s = null; s.length();
int[] a = new int[1]; a[1] = 1;
Map m = Map.of("a", 1); m.remove("a");
- Integer.parseInt("123.33");
3 / 0
java.lang.ArithmeticException
public class Main {
public static void main(String args[]) {
// System.out.println("x");
int a = 3 / 0;
}
}
-> Exception in thread "main" java.lang.ArithmeticException: / by zero at Main.main(Main.java:4)
String s = null; s.length();
java.lang.NullPointerException
public class Main {
public static void main(String args[]) {
// System.out.println("x");
String s = null; s.length();
}
}
-> xception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "<local1>" is null at Main.main(Main.java:4)
int[] a = new int[1]; a[1] = 1;
java.lang.ArithmeticException
public class Main {
public static void main(String args[]) {
// System.out.println("x");
// String s = null; s.length();
int[] a = new int[1];
a[1] = 1;
}
}
-> Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 at Main.main(Main.java:6)
Map m = Map.of("a", 1); m.remove("a");
java.lang.UnsupportedOperationException
import java.util.Map;
public class Main {
public static void main(String args[]) {
// System.out.println("x");
// String s = null; s.length();
// int[] a = new int[1];
// a[1] = 1;
Map m = Map.of("a", 1); m.remove("a");
}
}
Exception in thread "main" java.lang.UnsupportedOperationException
at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:127)
at java.base/java.util.ImmutableCollections$AbstractImmutableMap.remove(ImmutableCollections.java:910)
at Main.main(Main.java:9)
Integer.parseInt("123.33");
java.lang.NumberFormatException
public class Main {
public static void main(String args[]) {
// System.out.println("x");
// String s = null; s.length();
// int[] a = new int[1];
// a[1] = 1;
// Map m = Map.of("a", 1); m.remove("a");
Integer.parseInt("123.33");
}
}
Exception in thread "main" java.lang.NumberFormatException: For input string: "123.33"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at Main.main(Main.java:11)
Discussion