Sub topic 1
What Happens When Things Go Wrong
Programs don't always run as planned. A user types letters where a number belongs. A file is missing. Code tries to divide by zero. Java needs a way to report these problems, and that way is the exception.
What an exception is
An exception is an object that Java creates when something goes wrong while a program runs. It holds facts about the problem: what kind of problem it was and where it happened.
When Java creates an exception, we say the exception is thrown. Throwing an exception stops the normal flow of your code right away. The next line does not run. Java starts looking for code that can deal with the problem instead.
Why it matters
Without exceptions, a method would need a special return value, such as -1, to signal an error. Callers could forget to check it, and the program would keep going with bad data. An exception can't be ignored by accident. It either gets handled or it stops the program with a clear message.
How an exception travels
To see where an exception goes, you need to know about the call stack. The call stack is the list of methods that are running right now. When main calls average, and average calls divide, all three are on the stack, with divide on top.
When divide throws an exception, Java checks whether divide handles it. If not, divide ends at once and the exception moves to average. If average doesn't handle it either, the exception moves on to main. This movement up the call stack is called propagation.
If no method handles the exception, it is uncaught. Java then prints an error report and stops the program.
A worked example
Save this as Propagate.java, then compile and run it.
public class Propagate {
static int divide(int a, int b) {
return a / b;
}
static int average(int total, int count) {
int result = divide(total, count);
System.out.println("average computed");
return result;
}
public static void main(String[] args) {
System.out.println("Start");
System.out.println(average(10, 2));
System.out.println(average(10, 0));
System.out.println("End");
}
}The output looks like this:
Start
average computed
5
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Propagate.divide(Propagate.java:3)
at Propagate.average(Propagate.java:7)
at Propagate.main(Propagate.java:15)Here is what happened:
- The first call,
average(10, 2), works fine and prints5. - The second call reaches
10 / 0insidedivide. Integer division by zero is not allowed, so Java throws anArithmeticException. dividestops. The line"average computed"never prints for this call.- The exception travels up to
main, which also stops."End"never prints. - Java prints the exception type, its message (
/ by zero), and the methods it passed through.
The next chapters show how to catch exceptions so your program can recover and keep running.
Exercise
In the Propagate example, why does the line "average computed" print once and not twice?
Show answer
The first call to average succeeds, so the line prints. In the second call, divide throws an exception before average reaches that line, so average stops at once and the rest of its code is skipped.
Exercise
What happens when an exception propagates all the way out of main without being handled?
Show answer
The exception is uncaught. Java prints the exception type, its message, and a stack trace, and then the program stops.