Challenge - 5 Problems
Throw Keyword 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 throw?
Consider the following Java code snippet. What will be printed when it runs?
Java
public class TestThrow { public static void checkAge(int age) { if (age < 18) { throw new IllegalArgumentException("Age must be at least 18"); } else { System.out.println("Access granted"); } } public static void main(String[] args) { try { checkAge(16); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } } }
Attempts:
2 left
π‘ Hint
Look at what happens when age is less than 18 and how the exception is handled.
β Incorrect
The method throws an IllegalArgumentException when age is less than 18. The main method catches this exception and prints its message, which is "Age must be at least 18".
β Predict Output
intermediate2:00remaining
What happens when throw is used without catch?
What will be the output or result of this Java program?
Java
public class ThrowWithoutCatch { public static void main(String[] args) { throw new RuntimeException("Error occurred"); } }
Attempts:
2 left
π‘ Hint
RuntimeException is unchecked. What happens if it is thrown but not caught?
β Incorrect
Throwing a RuntimeException without catching it causes the program to terminate and print the exception's stack trace and message.
π§ Debug
advanced2:00remaining
Identify the error in this throw usage
This Java code tries to throw an exception. What is the error?
Java
public class ThrowError { public static void main(String[] args) { throw new Exception("Checked exception"); } }
Attempts:
2 left
π‘ Hint
Exception is a checked exception. What does Java require for checked exceptions?
β Incorrect
Checked exceptions must be either caught or declared to be thrown. This code neither catches nor declares the exception, causing a compilation error.
π§ Conceptual
advanced2:00remaining
What is the effect of throw inside a method?
When a method uses the throw keyword to throw an exception, what happens immediately after the throw statement executes?
Attempts:
2 left
π‘ Hint
Think about what throw does to the normal flow of a program.
β Incorrect
Throwing an exception immediately stops the current method execution and transfers control to the nearest matching catch block or terminates the program if none is found.
β Predict Output
expert3:00remaining
What is the output of nested throw and catch blocks?
Analyze the output of this Java program with nested try-catch and throw statements.
Java
public class NestedThrowCatch { public static void main(String[] args) { try { try { throw new IllegalArgumentException("Inner exception"); } catch (NullPointerException e) { System.out.println("Caught NullPointerException"); } } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } } }
Attempts:
2 left
π‘ Hint
Which catch block matches the thrown exception type?
β Incorrect
The inner try throws IllegalArgumentException, which is not caught by the inner catch (which catches NullPointerException). The outer catch catches IllegalArgumentException and prints its message.