Defensive Programming
Learn defensive programming: validate inputs, avoid nulls, and fail early with clear exceptions.
Defensive Programming is a free Java Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intro
Defensive programming means writing code that anticipates errors: invalid input, nulls, wrong states. It helps catch issues early.
Validate inputs
Check arguments at the start of methods. If input is invalid, throw an exception immediately instead of continuing with bad data.
Code: validate input
This method checks the denominator and throws an exception instead of dividing by zero.
public class Main {
static int divide(int a, int b) {
if (b == 0) {
throw new IllegalArgumentException("Denominator must not be zero");
}
return a / b;
}
public static void main(String[] args) {
try {
System.out.println(divide(10, 0));
} catch (IllegalArgumentException e) {
System.out.println("Caught: " + e.getMessage());
}
}
}
Avoid nulls
Avoid passing or returning null. Instead, use empty collections, default values, or Optional to reduce NullPointerExceptions.
Code: null check
Here we check for null and throw an exception instead of letting a NullPointerException occur later.
public class Main {
static String upper(String s) {
if (s == null) {
throw new IllegalArgumentException("String must not be null");
}
return s.toUpperCase();
}
public static void main(String[] args) {
try {
System.out.println(upper(null));
} catch (IllegalArgumentException e) {
System.out.println("Caught: " + e.getMessage());
}
}
}
Code: fail fast
Fail fast: detect problems immediately with clear errors. This prevents confusing bugs later.
public class Main {
static int[] makeArray(int size) {
if (size < 0) {
throw new IllegalArgumentException("Size must be non-negative");
}
return new int[size];
}
public static void main(String[] args) {
try {
makeArray(-5);
} catch (IllegalArgumentException e) {
System.out.println("Caught: " + e.getMessage());
}
}
}
Defensive programming check
Quick check: What is a key goal of defensive programming?
Recap
Recap: Defensive programming validates inputs, avoids nulls, and fails fast. It makes programs safer and easier to debug.
Frequently asked questions
Is the “Defensive Programming” lesson free?
Yes — the full text of “Defensive Programming” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Defensive Programming”?
Learn defensive programming: validate inputs, avoid nulls, and fail early with clear exceptions. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Defensive Programming” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Throwing & Custom Exceptions
- try-with-resources (AutoCloseable)
- Defensive Programming