Challenge - 5 Problems
Throws Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
What is the output of this Java code using throws?
Consider the following Java code snippet. What will be printed when the main method runs?
Java
import java.io.*; public class TestThrows { static void readFile() throws IOException { throw new IOException("File error"); } public static void main(String[] args) { try { readFile(); } catch (IOException e) { System.out.println("Caught: " + e.getMessage()); } } }
Attempts:
2 left
π‘ Hint
Look at how the exception thrown by readFile() is handled in main.
β Incorrect
The method readFile() declares it throws IOException. main calls readFile() inside a try block and catches IOException, printing the message. So the output is the caught message.
β Predict Output
intermediate2:00remaining
What happens if a method declares throws but caller does not handle?
What will happen when running this Java code?
Java
public class ThrowsTest { static void risky() throws Exception { throw new Exception("Risky exception"); } public static void main(String[] args) { risky(); System.out.println("After risky call"); } }
Attempts:
2 left
π‘ Hint
Check if main handles or declares the exception thrown by risky().
β Incorrect
The method risky() throws Exception. main calls risky() but does not handle or declare the exception, so compilation fails with unhandled exception error.
π§ Debug
advanced2:00remaining
Identify the error in this throws usage
What error does this code produce when compiled?
Java
import java.io.IOException; public class ThrowsDemo { void method() throws IOException { System.out.println("Inside method"); } void caller() { method(); } }
Attempts:
2 left
π‘ Hint
Check if caller() handles or declares the exception thrown by method().
β Incorrect
method() declares it throws IOException, a checked exception. caller() calls method() but neither catches nor declares the exception, so compilation error occurs.
π§ Conceptual
advanced1:30remaining
Which statement about throws keyword is true?
Select the correct statement about the throws keyword in Java.
Attempts:
2 left
π‘ Hint
Think about what throws does in a method signature.
β Incorrect
The throws keyword declares that a method might throw exceptions, passing responsibility to the caller to handle them.
β Predict Output
expert3:00remaining
What is the output of this nested throws and try-catch code?
Analyze the output of this Java program:
Java
import java.io.IOException; public class NestedThrows { static void inner() throws IOException { throw new IOException("Inner exception"); } static void outer() throws IOException { try { inner(); } catch (IOException e) { System.out.println("Caught in outer: " + e.getMessage()); throw e; } finally { System.out.println("Finally block executed"); } } public static void main(String[] args) { try { outer(); } catch (IOException e) { System.out.println("Caught in main: " + e.getMessage()); } } }
Attempts:
2 left
π‘ Hint
Remember that finally block always runs, and exceptions rethrown propagate up.
β Incorrect
inner() throws IOException caught in outer(), prints message, then rethrows. finally block runs printing its message. main catches rethrown exception and prints its message.