Learn Java
import com.learnjava.exceptionhandling.BuiltinExceptions;

Built-in Exception Types in Java

@Concept

Java's standard library ships a large set of ready-made exception types covering common failure situations, so most everyday problems don't require defining a custom exception at all: NullPointerException for calling a method or accessing a field on a null reference, ArrayIndexOutOfBoundsException for an invalid array index, ArithmeticException for something like integer division by zero, ClassCastException for an invalid cast between incompatible types, and NumberFormatException for failing to parse text as a number.

Reaching for the most specific, already-accurate built-in exception type — rather than a generic RuntimeException with just a custom message — gives calling code a much more useful signal, since catching code can distinguish a NumberFormatException from a NullPointerException without inspecting message text at all. A well-chosen existing type also means less code to write and maintain than a needless custom one.

@HowItWorks
1

NullPointerException

Thrown when calling a method or accessing a field on a reference that's actually null.

2

ArrayIndexOutOfBoundsException

Thrown when an array is accessed with an index outside its valid range.

3

ArithmeticException

Thrown for invalid arithmetic, most commonly integer division by zero.

4

ClassCastException

Thrown when an invalid cast is attempted between two incompatible types at runtime.

5

NumberFormatException

Thrown when text that doesn't represent a valid number is parsed as one, like Integer.parseInt("abc").

@Example
public class BuiltinExceptionDemo {
public static void main(String[] args) {
try {
int value = Integer.parseInt("not a number");
} catch (NumberFormatException e) {
System.out.println("Caught: " + e.getMessage());
}
}
}
Output
Caught: For input string: "not a number"
1Integer.parseInt("not a number") tries to interpret text that isn't a valid number at all.
2Java throws a NumberFormatException specifically, rather than some generic error, precisely naming what went wrong.
3The catch block matches that specific exception type directly.
4getMessage() returns text describing exactly which input string failed to parse, which the single println call then prints.
@UseCases
Catching NumberFormatException specifically when parsing user-supplied text as a number
Recognizing a NullPointerException's stack trace as pointing to a specific null reference that was used unsafely
Choosing an existing, precise built-in exception type instead of writing an unnecessary custom one
Distinguishing several different built-in exception types by catching each one specifically rather than one generic Exception

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.