0
0
Javaprogramming~5 mins

Exception propagation in Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AIgnoring exceptions silently
BAutomatically fixing exceptions
CCatching exceptions only in the main method
DPassing an exception up the call stack until caught
If a method throws an exception but does not catch it, what happens?
AThe exception is logged automatically
BThe program crashes immediately
CThe exception is passed to the caller method
DThe exception is ignored
What happens if no method catches a thrown exception?
AThe exception is discarded
BThe program terminates and prints the stack trace
CThe program continues normally
DThe exception is converted to a warning
Which keyword is used to handle exceptions in Java?
Atry-catch
Bthrow-catch
Ccatch-throw
Dhandle-exception
Why might you want to let an exception propagate instead of catching it immediately?
ATo handle errors in a central place
BTo hide errors from users
CTo speed up the program
DTo avoid writing any error handling code
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.