Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.
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
✗ 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?
AThe exception is logged automatically
BThe program crashes immediately
CThe exception is passed to the caller method
DThe exception is ignored
✗ Incorrect
The exception is passed to the caller method to handle or propagate further.
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
✗ Incorrect
If uncaught, the program stops and prints the exception details to help debugging.
Which keyword is used to handle exceptions in Java?
Atry-catch
Bthrow-catch
Ccatch-throw
Dhandle-exception
✗ 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?
ATo handle errors in a central place
BTo hide errors from users
CTo speed up the program
DTo avoid writing any error handling code
✗ 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.
Practice
(1/5)
1. What does exception propagation mean in Java?
easy
A. An exception is passed up the call stack until caught or program ends
B. An exception is ignored and the program continues normally
C. An exception is automatically fixed by the JVM
D. An exception is converted into a warning message
Solution
Step 1: Understand exception propagation concept
When an exception occurs, Java looks for a matching catch block in the current method. If none is found, it passes the exception to the caller method.
Step 2: Follow the exception up the call stack
This passing continues up the call stack until a catch block handles it or the program terminates if uncaught.
Final Answer:
An exception is passed up the call stack until caught or program ends -> Option A
Quick Check:
Exception moves up call stack = A [OK]
Hint: Exception moves up call stack until caught [OK]
Common Mistakes:
Thinking exceptions are ignored automatically
Believing JVM fixes exceptions silently
Confusing exceptions with warnings
2. Which of the following is the correct way to declare a method that might throw an exception?
easy
A. public void readFile() throws IOException {}
B. public void readFile() throw IOException {}
C. public void readFile() throws IOException() {}
D. public void readFile() throws-IOException {}
Solution
Step 1: Recall correct syntax for throws clause
The keyword throws is used followed by the exception class name without parentheses.
Step 2: Check each option syntax
public void readFile() throws IOException {} uses correct syntax: throws IOException. The other options have syntax errors.
Final Answer:
public void readFile() throws IOException {} -> Option A
Quick Check:
Correct throws syntax = B [OK]
Hint: Use 'throws' keyword followed by exception class name [OK]
Common Mistakes:
Writing 'throw' instead of 'throws'
Adding parentheses after exception name
Using invalid symbols like '-'
3. What will be the output of the following code?
public class Test {
static void method() throws Exception {
throw new Exception("Error occurred");
}
public static void main(String[] args) {
try {
method();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
medium
A. Compilation error
B. Exception in thread "main" java.lang.Exception
C. No output
D. Error occurred
Solution
Step 1: Analyze method throwing exception
The method explicitly throws a new Exception with message "Error occurred".
Step 2: Check exception handling in main
The main method calls method() inside try-catch. The catch block prints the exception message.
Final Answer:
Error occurred -> Option D
Quick Check:
Exception caught and message printed = C [OK]
Hint: Exception message prints if caught in try-catch [OK]
Common Mistakes:
Assuming uncaught exception causes crash
Confusing exception message with full stack trace
Thinking code won't compile without throws in main
4. Identify the error in this code snippet:
public class Demo {
static void risky() {
throw new IOException("IO error");
}
public static void main(String[] args) {
risky();
}
}
medium
A. main method must catch IOException
B. IOException cannot be thrown directly
C. Method risky() must declare 'throws IOException'
D. No error, code is correct
Solution
Step 1: Check exception type thrown
IOException is a checked exception and must be declared or caught.
Step 2: Verify method declaration
Method risky() throws IOException but does not declare it with 'throws' keyword, causing a compile error.
Final Answer:
Method risky() must declare 'throws IOException' -> Option C
Quick Check:
Checked exceptions require throws declaration = A [OK]
Hint: Checked exceptions need 'throws' or try-catch [OK]
Common Mistakes:
Ignoring throws declaration for checked exceptions
Thinking IOException is unchecked
Assuming main must catch exception always
5. Consider this code:
class A {
void process() throws Exception {
throw new Exception("Error in A");
}
}
class B extends A {
@Override
void process() throws Exception {
super.process();
}
}
public class Main {
public static void main(String[] args) {
A obj = new B();
try {
obj.process();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
What will be the output and why?
hard
A. Runtime error due to invalid override
B. Error in A, because exception propagates from superclass method
C. Compilation error due to throws mismatch
D. No output, exception is swallowed silently
Solution
Step 1: Understand method overriding with exceptions
Subclass B overrides process() and calls super.process(), which throws Exception.
Step 2: Exception propagates to main and is caught
Main calls obj.process() on B instance, exception thrown by A.process() propagates and is caught in main's try-catch, printing the message.
Final Answer:
Error in A, because exception propagates from superclass method -> Option B
Quick Check:
Exception propagates through override and caught = D [OK]
Hint: Overridden method can throw same exceptions up [OK]
Common Mistakes:
Thinking override cannot throw exceptions declared in superclass