Sobes.tech
Junior

What unchecked exceptions exist in Java?

sobes.tech AI

Answer from AI

In Java, there are several standard unchecked exceptions that inherit from RuntimeException or Error. Some of the most common ones:

  • ArithmeticException: Occurs during invalid arithmetic operations, such as division by zero.
  • ArrayIndexOutOfBoundsException: Happens when attempting to access an array element with an invalid index (negative or out of bounds).
  • ClassCastException: Thrown when trying to cast an object to an incompatible type.
  • IllegalArgumentException: Occurs when a method is called with an invalid argument.
  • IllegalStateException: Indicates that the object's current state does not allow the requested operation.
  • IndexOutOfBoundsException: A more general exception that occurs when accessing an index outside the valid range (e.g., for lists or strings).
  • NullPointerException: Thrown when attempting to use a null reference where an object is required.
  • NumberFormatException: Happens when trying to convert a string to a number, but the string does not contain a valid numeric representation.
  • SecurityException: Thrown by the SecurityManager to indicate a security violation.
  • UnsupportedOperationException: Indicates that the requested operation is not supported by the object.
  • OutOfMemoryError: A subclass of Error, indicating that the JVM has run out of memory to allocate an object.
  • StackOverflowError: Also a subclass of Error, indicating a stack overflow, usually due to deep or infinite recursion.

Unchecked exceptions do not require mandatory handling or declaration in the method signature (throws) because they typically indicate programming errors or unforeseen runtime issues that cannot be effectively recovered from.