An exception is easier to understand when you can point to the line where ordinary execution stops. This small Java exercise asks you to predict a printed trace, run it in your own practice environment and explain how control reaches a handler in the calling method.
Use a local practice project with a Java Development Kit. This is an original teaching example, not a production input-validation design or a promised assignment in every Coding Bytes batch. Record your JDK version and keep prediction and observation in separate columns.
Draw the calls before running the code
The call sequence is main → service → parse. In the first round, the text passed to Integer.parseInt is deliberately not an integer. The example uses NumberFormatException, an unchecked exception, so its methods do not need a checked-exception throws declaration.
Read Oracle’s exception introduction and catch-block explanation alongside the exercise. A compatible handler can be in a caller; a method call does not automatically turn a failure into an ordinary return value.
Round one: predict the printed trace
Save the following as PropagationDemo.java. Before executing it, write down which printed lines you expect, in order. Put a mark beside any line you believe will be skipped.
public class PropagationDemo {
static void parse() {
System.out.println("parse:start");
Integer.parseInt("eight");
System.out.println("parse:end");
}
static void service() {
System.out.println("service:start");
try {
parse();
System.out.println("service:end");
} finally {
System.out.println("service:finally");
}
}
public static void main(String[] args) {
System.out.println("main:start");
try {
service();
System.out.println("main:after");
} catch (NumberFormatException ex) {
System.out.println("main:catch");
}
System.out.println("main:end");
}
}From that practice directory, compile with javac PropagationDemo.java, then run java PropagationDemo. If the compiler is unavailable, set up the JDK for your practice environment first; a missing command is an environment result, not evidence about exception propagation.
Compare with the expected first-round trace
main:start
service:start
parse:start
service:finally
main:catch
main:endThe conversion throws before parse:end. Execution does not resume at the next line in parse, or at service:end. The finally block in service runs as control leaves its try block. The matching catch in main handles the exception, after which execution reaches main:end. The statement main:after inside the interrupted try block is skipped.
Round two: change one input
Replace only "eight" with "8", then compile again and run. Keep the original prediction beside a new one. The normal trace should now include parse:end, service:end, service:finally, main:after and main:end, following the three start lines. The catch marker should be absent.
Oracle’s finally reference explains why this block runs when the try block exits normally as well as during exception handling. It also notes an important limit: JVM exit can prevent finally from running. This exercise has no JVM-exit call and makes no universal guarantee about every termination scenario.
Copy this observation sheet
| Question | First input | Changed input |
|---|---|---|
| What is the first line that cannot finish normally? | Write your prediction and actual observation. | Record whether conversion completes. |
| Which end or after markers appear? | List exact lines, not “it crashed”. | Compare with the normal path. |
| Does finally run? | Record its position. | Record its position again. |
| Where is the exception handled? | Name the method and catch type. | Record that the handler is not entered. |
Explain it without hiding the result
If your trace differs, first check that you recompiled the edited file and ran the correct class. Keep the original output, including any compiler error. Avoid inserting catch-all blocks merely to make the example finish; doing so changes the question being tested.
Does finally replace catch? No. It supplies code on leaving the try/catch structure; a matching catch is what handles this exception. Is printing a message enough for a real application? This exercise only traces control. A real boundary needs an intentional error response and appropriate handling of private data.
Bring both traces and your explanation to a review. Explore Java training or ask about a free demo to discuss your starting level and the current course exercises.
