Un-checked Exceptions
public class NullPointerExceptionExample { public static void main(String[] args) { String str = null; int length = str.length(); // This will throw a NullPointerException } }public class ArrayIndexOutOfBoundsExceptionExample { public static void main(String[] args) { int[] arr = {1, 2, 3}; int value = arr[5]; // This will throw an ArrayIndexOutOfBoundsException } }public class ArithmeticExceptionExample { public static void main(String[] args) { int result = 5 / 0; // This will throw an ArithmeticException } }public class NumberFormatExceptionExample { public static void main(String[] args) { int num = Integer.parseInt("abc"); // This will throw a NumberFormatException } }public class IllegalArgumentExceptionExample { public static void main(String[] args) { int age = -5; if (age < 0) { throw new IllegalArgumentException("Age cannot be negative."); } } }public class ClassCastExceptionExample { public static void main(String[] args) { Object obj = "Hello"; Integer num = (Integer) obj; // This will throw a ClassCastException } }
Last updated