Chapter 47: What is Exception in Java
So far, most programs assumed inputs were kind and math always worked. Real software breaks those assumptions. Java signals that break with an exception: a normal Java object that interrupts the current path and hands control to code that knows how to react—or exits if nobody does.
try / catch lands in Chapter 48. finally in Chapter 49.
1. Topic title
Exception: an object that says “this path cannot continue as written”
2. What it means
When something impossible-for-this-method happens—divide by zero, null where an object was required, illegal argument—you can throw an exception object. The runtime searches upward for a matching catch. If none appears, the thread stops and prints a stack trace: a list of which methods called which, newest frames near the top.
Exceptions are not mysterious signals from the hardware. They are instances of classes (IllegalArgumentException, ArithmeticException, …) with messages you can read.
3. Why it matters
Return codes everywhere (int where -1 means error) rot quickly—callers forget to check. Exceptions make “something went wrong” a first-class path you cannot ignore as easily.
4. Tiny mental model
Picture a relay race. The baton is normal return values. An exception is a whistle: everyone stops the usual handoff until a coach (catch) decides the next step.
5. Java code example
public class ExceptionIntroDemo {
static int half(int value) {
if (value < 0) {
throw new IllegalArgumentException("need non-negative, got " + value);
}
return value / 2;
}
public static void main(String[] args) {
System.out.println(half(10));
System.out.println(half(-3));
}
}Running this prints 5 then blows up with IllegalArgumentException because half refuses negative input on purpose.
6. Explanation of code
throw new IllegalArgumentException(...) builds the object right now and abandons the rest of half—no return runs after a throw in the same block. The message string is for humans reading logs.
7. Common mistakes
Catching everything with catch (Exception e) and then silencing it—hides real defects.
Throwing for normal control flow (“find index, throw if missing” in a tight loop) can be slower and noisier than returning OptionalInt or a sentinel when appropriate.
Vague messages like "bad"—future-you needs numbers and names in the string.
8. Practical habits
When an exception prints in your console, read the top few lines slowly: exception type, message, then your package names in the stack. That order already tells you what and where before you change any code.
9. Small practice task
Write static int safeIndex(String[] rows, int i) that throws ArrayIndexOutOfBoundsException with a message showing i and array length when i is out of range; otherwise returns rows[i].length().
Beginner tip
Exceptions feel scary because they stop the program. That is also why they are useful: they refuse to continue with nonsense data. Chapter 48 shows how to recover gracefully when recovery makes sense.