Bird
Raised Fist0
Javaprogramming~20 mins

Exception propagation in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
πŸŽ–οΈ
Exception Propagation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What 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");
    }
}
ACaught in main: null
BException in thread "main" java.lang.Exception: Error in methodB
CCaught in main: Error in methodB
DNo output
Attempts:
2 left
πŸ’‘ Hint
Trace how the exception thrown in methodB moves up to main and is caught there.
❓ Predict Output
intermediate
2:00remaining
What 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");
    }
}
ACompilation error: unhandled exception Exception
BRuntime exception thrown: Exception
CProgram runs and prints nothing
DCompilation error: methodB not found
Attempts:
2 left
πŸ’‘ Hint
Checked exceptions must be declared or caught.
πŸ”§ Debug
advanced
2:00remaining
Why 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");
    }
}
Amain does not catch the exception thrown by methodB
BmethodA does not declare 'throws Exception' but calls methodB which throws Exception
CmethodB does not throw any exception
DmethodA catches the exception but does not rethrow it
Attempts:
2 left
πŸ’‘ Hint
Check methodA's signature and its call to methodB.
❓ Predict Output
advanced
2:00remaining
What 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");
    }
}
ACaught in main: null
BCaught in methodA: Error in methodB
CNo output
DCaught in main: Error in methodB
Attempts:
2 left
πŸ’‘ Hint
Check which exception type is caught in methodA and which propagates to main.
🧠 Conceptual
expert
3:00remaining
How 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");
    }
}
A2
B1
C3
D0
Attempts:
2 left
πŸ’‘ Hint
Count each catch block that prints a message during propagation.

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

  1. 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.
  2. 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.
  3. Final Answer:

    An exception is passed up the call stack until caught or program ends -> Option A
  4. 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

  1. Step 1: Recall correct syntax for throws clause

    The keyword throws is used followed by the exception class name without parentheses.
  2. Step 2: Check each option syntax

    public void readFile() throws IOException {} uses correct syntax: throws IOException. The other options have syntax errors.
  3. Final Answer:

    public void readFile() throws IOException {} -> Option A
  4. 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

  1. Step 1: Analyze method throwing exception

    The method explicitly throws a new Exception with message "Error occurred".
  2. Step 2: Check exception handling in main

    The main method calls method() inside try-catch. The catch block prints the exception message.
  3. Final Answer:

    Error occurred -> Option D
  4. 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

  1. Step 1: Check exception type thrown

    IOException is a checked exception and must be declared or caught.
  2. Step 2: Verify method declaration

    Method risky() throws IOException but does not declare it with 'throws' keyword, causing a compile error.
  3. Final Answer:

    Method risky() must declare 'throws IOException' -> Option C
  4. 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

  1. Step 1: Understand method overriding with exceptions

    Subclass B overrides process() and calls super.process(), which throws Exception.
  2. 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.
  3. Final Answer:

    Error in A, because exception propagates from superclass method -> Option B
  4. 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
  • Assuming exception is lost in subclass
  • Confusing compile error with runtime behavior