Recall & Review
beginner
What is exception propagation in Java?
Exception propagation is the process where an exception is passed up the call stack until it is caught and handled by an appropriate catch block.
Click to reveal answer
beginner
How does Java handle an exception if it is not caught in the current method?
If an exception is not caught in the current method, Java automatically passes it to the caller method. This continues until the exception is caught or the program terminates.
Click to reveal answer
beginner
What happens if an exception is not caught anywhere in the call stack?
If an exception is not caught anywhere, the Java runtime system terminates the program and prints the exception's stack trace to help debug the error.
Click to reveal answer
intermediate
Consider this code snippet:<br>
void methodA() { methodB(); }<br>void methodB() { throw new RuntimeException(); }<br>What happens when methodA is called?When methodA calls methodB, methodB throws a RuntimeException. Since methodB does not catch it, the exception propagates back to methodA. If methodA also does not catch it, it continues propagating up the call stack.
Click to reveal answer
intermediate
Why is it useful to let exceptions propagate instead of catching them immediately?Letting exceptions propagate allows higher-level methods to handle errors in a centralized way, improving code clarity and avoiding repetitive error handling in every method.
Click to reveal answer
What does exception propagation mean in Java?
✗ Incorrect
Exception propagation means the exception moves up the call stack until a matching catch block handles it.
If a method throws an exception but does not catch it, what happens?
✗ Incorrect
The exception is passed to the caller method to handle or propagate further.
What happens if no method catches a thrown exception?
✗ Incorrect
If uncaught, the program stops and prints the exception details to help debugging.
Which keyword is used to handle exceptions in Java?
✗ Incorrect
The try-catch block is used to catch and handle exceptions.
Why might you want to let an exception propagate instead of catching it immediately?
✗ Incorrect
Centralized error handling improves code clarity and maintenance.
Explain how exception propagation works in Java with an example.
Think about what happens when a method throws an exception but does not catch it.
You got /4 concepts.
Why is it sometimes better to let exceptions propagate instead of catching them immediately?
Consider how catching exceptions in many places can affect code readability.
You got /4 concepts.