BackendBench
Coffee

Unravel › Java concepts › Errors and logging

Errors and logging

The exceptions you meet every day

A short list of exceptions causes most production errors: NullPointerException, IndexOutOfBoundsException, ClassCastException, NumberFormatException, IllegalArgumentException, IllegalStateException, UnsupportedOperationException, StackOverflowError and OutOfMemoryError.

Learn these first

At a glance

Mental model

The common warning lights on a car dashboard: each one points to a typical cause, and learning the meaning of the five or six common ones solves most breakdowns.

What happens, in order
  1. 1NullPointerException: something is null that you called a method on or unboxedThe message names the variable (helpful NPE messages since Java 14). Find where it should have been set.
  2. 2IndexOutOfBoundsException / ArrayIndexOutOfBoundsException: index below 0 or at least the size; check < versus <=
  3. 3ClassCastException: a cast to the wrong type, often from raw types or mixed collectionsNumberFormatException: bad text for parseInt. ArithmeticException: integer division by zero.
  4. 4IllegalArgumentException (the caller passed a bad argument) and IllegalStateException (the object is in the wrong state for this call) are the standard ways to reject misuseUnsupportedOperationException appears on immutable collections (List.of) and read-only views.
  5. 5NoSuchElementException: Optional.get() on empty, iterator.next() at the end, Scanner without input
Remember
Watch out
Catching NullPointerException instead of fixing the nullCatching Throwable, which also catches OutOfMemoryErrorCalling Optional.get() without checkingAssuming List.of returns a mutable list
Example (highlighted lines explain it)
import java.util.*;public class Safe {  static int parse(String s) {    try { return Integer.parseInt(s.trim()); }    catch (NumberFormatException e) { throw new IllegalArgumentException("not a number: " + s, e); }  }  static <T> T first(List<T> xs) {    try { return xs.get(0); }    catch (IndexOutOfBoundsException e) { throw new NoSuchElementException("empty list"); }  }  static void fill(List<String> l) { try { l.add("x"); } catch (UnsupportedOperationException e) { System.err.println("read-only list"); } }}
  1. line 5 uses NumberFormatException, the core of The exceptions you meet every day
  2. line 9 uses IndexOutOfBoundsException, the core of The exceptions you meet every day
  3. line 11 uses UnsupportedOperationException, the core of The exceptions you meet every day
Read the full explanation (about 2 min)

A short list of exceptions causes most production errors: NullPointerException, IndexOutOfBoundsException, ClassCastException, NumberFormatException, IllegalArgumentException, IllegalStateException, UnsupportedOperationException, StackOverflowError and OutOfMemoryError.

A mental model

The common warning lights on a car dashboard: each one points to a typical cause, and learning the meaning of the five or six common ones solves most breakdowns.

How it works

NullPointerException: something is null that you called a method on or unboxed. The message names the variable (helpful NPE messages since Java 14). Find where it should have been set.

IndexOutOfBoundsException / ArrayIndexOutOfBoundsException: index below 0 or at least the size; check < versus <=.

ClassCastException: a cast to the wrong type, often from raw types or mixed collections. NumberFormatException: bad text for parseInt. ArithmeticException: integer division by zero.

IllegalArgumentException (the caller passed a bad argument) and IllegalStateException (the object is in the wrong state for this call) are the standard ways to reject misuse. UnsupportedOperationException appears on immutable collections (List.of) and read-only views.

NoSuchElementException: Optional.get() on empty, iterator.next() at the end, Scanner without input.

Going deeper

StackOverflowError is an Error from recursion that never ends (missing base case) or too deep. OutOfMemoryError means the heap (or metaspace, or native memory, or thread count) ran out: usually a leak or an unbounded cache or queue, rarely just "need a bigger heap". Do not catch Error to continue.

NoClassDefFoundError means the class existed at compile time but cannot load now (missing jar, or a static initialiser failed earlier: look for ExceptionInInitializerError). ClassNotFoundException is the checked version from reflection.

Throw the standard exceptions in your own code rather than inventing new ones for argument and state checks; callers and tools recognise them.

Under the hood

Most of these are thrown by the JVM itself (null dereference, bounds check, division, class cast), not by library code, which is why their stack traces point directly at your line. Error subclasses signal problems in the JVM or the environment that applications should rarely handle.

Common mistakes

  • Catching NullPointerException instead of fixing the null.
  • Catching Throwable, which also catches OutOfMemoryError.
  • Calling Optional.get() without checking.
  • Assuming List.of returns a mutable list.

How Unravel recognises it

When you paste code, Unravel looks for these signs of the exceptions you meet every day. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is The exceptions you meet every day?

A short list of exceptions causes most production errors: NullPointerException, IndexOutOfBoundsException, ClassCastException, NumberFormatException, IllegalArgumentException, IllegalStateException, UnsupportedOperationException, StackOverflowError and OutOfMemoryError.

What are the most common mistakes with The exceptions you meet every day?

Catching NullPointerException instead of fixing the null. Catching Throwable, which also catches OutOfMemoryError. Calling Optional.get() without checking. Assuming List.of returns a mutable list.

How can I see The exceptions you meet every day in real code?

Paste your own Java into Unravel and it will point out where the exceptions you meet every day appears, explain how it works at the detail level you choose, and draw the flow of the code around it.