Challenge - 5 Problems
Exception Propagation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediateWhat is the output of this Java code with exception propagation?
Consider the following Java code. What will be printed when the main method runs?
Java
public class Test { public static void main(String[] args) { try { methodA(); } catch (Exception e) { System.out.println("Caught in main: " + e.getMessage()); } } static void methodA() throws Exception { methodB(); } static void methodB() throws Exception { throw new Exception("Error in methodB"); } }
Attempts:
2 left
π‘ Hint
Trace how the exception thrown in methodB moves up to main and is caught there.
β Incorrect
The exception is thrown in methodB, propagated through methodA, and caught in main's catch block, printing the message.
β Predict Output
intermediateWhat happens if a checked exception is not declared or caught?
What error will the following Java code produce when compiled?
Java
public class Test { public static void main(String[] args) { methodA(); } static void methodA() { methodB(); } static void methodB() throws Exception { throw new Exception("Error"); } }
Attempts:
2 left
π‘ Hint
Checked exceptions must be declared or caught.
β Incorrect
methodA calls methodB which throws a checked exception but does not declare or catch it, causing a compile-time error.
π§ Debug
advancedWhy does this code cause a compilation error related to exception propagation?
Identify the cause of the compilation error in this code and select the correct explanation.
Java
public class Test { public static void main(String[] args) { try { methodA(); } catch (Exception e) { System.out.println("Caught: " + e.getMessage()); } } static void methodA() { methodB(); } static void methodB() throws Exception { throw new Exception("Error"); } }
Attempts:
2 left
π‘ Hint
Check methodA's signature and its call to methodB.
β Incorrect
methodA calls methodB which throws a checked exception but methodA does not declare it or catch it, causing a compile-time error.
β Predict Output
advancedWhat is the output when multiple exceptions propagate and are caught?
What will be printed when this Java program runs?
Java
public class Test { public static void main(String[] args) { try { methodA(); } catch (Exception e) { System.out.println("Caught in main: " + e.getMessage()); } } static void methodA() throws Exception { try { methodB(); } catch (NullPointerException e) { System.out.println("Caught in methodA: " + e.getMessage()); } } static void methodB() throws Exception { throw new Exception("Error in methodB"); } }
Attempts:
2 left
π‘ Hint
Check which exception type is caught in methodA and which propagates to main.
β Incorrect
methodB throws Exception which is not caught by methodA's catch for NullPointerException, so it propagates to main and is caught there.
π§ Conceptual
expertHow many exceptions are caught and printed in this nested propagation example?
Given the code below, how many times will a message be printed to the console?
Java
public class Test { public static void main(String[] args) { try { methodA(); } catch (Exception e) { System.out.println("Caught in main: " + e.getMessage()); } } static void methodA() throws Exception { try { methodB(); } catch (Exception e) { System.out.println("Caught in methodA: " + e.getMessage()); throw e; } } static void methodB() throws Exception { throw new Exception("Error in methodB"); } }
Attempts:
2 left
π‘ Hint
Count each catch block that prints a message during propagation.
β Incorrect
methodB throws Exception caught in methodA which prints once, then rethrows it to main which catches and prints again, total 2 prints.
