ProLesson 5.5Intermediate3 min readUpdated 30 October 2026
Exception best practices and common mistakes
The habits that separate solid production code from fragile code, including avoiding NullPointerException, logging properly, and failing fast.
You now know the mechanics. This lesson is about judgement: how experienced developers use exceptions so problems are easy to find and fix.
011. Fail fast with clear messages
Check inputs at the start of a method and throw immediately with a message that says what was wrong and which value caused it:
FailFast.java
import java.util.Objects;
public class FailFast {
static String initials(String fullName) {
Objects.requireNonNull(fullName, "fullName must not be null");
if (fullName.isBlank()) {
throw new IllegalArgumentException("fullName must not be blank");
}
StringBuilder sb = new StringBuilder();
for (String part : fullName.trim().split("\\s+")) {
sb.append(part.charAt(0));
}
return sb.toString().toUpperCase();
}
public static void main(String[] args) {
System.out.println(initials("asha devi rao"));
try {
initials(null);
} catch (NullPointerException e) {
System.out.println(e.getMessage());
}
}
}ADR fullName must not be null
An error close to its cause is much easier to fix than a mysterious crash ten methods later.
Loading the rest of the lesson…