0
0
Javaprogramming~20 mins

Throw keyword in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Throw Keyword Mastery
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 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());
        }
    }
}
AAge must be at least 18
BException in thread "main" java.lang.IllegalArgumentException: Age must be at least 18
CAccess granted
DCompilation error
Attempts:
2 left
πŸ’‘ Hint
Look at what happens when age is less than 18 and how the exception is handled.
❓ Predict Output
intermediate
2: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");
    }
}
AProgram compiles but throws RuntimeException and terminates with stack trace
BProgram runs silently with no output
CCompilation error: unhandled exception
DProgram prints: Error occurred
Attempts:
2 left
πŸ’‘ Hint
RuntimeException is unchecked. What happens if it is thrown but not caught?
πŸ”§ Debug
advanced
2: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");
    }
}
ASyntax error: throw keyword used incorrectly
BCompilation error: unhandled checked exception
CRuntime error: Exception thrown but not caught
DNo error, code runs fine
Attempts:
2 left
πŸ’‘ Hint
Exception is a checked exception. What does Java require for checked exceptions?
🧠 Conceptual
advanced
2: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?
AThe method pauses and waits for user input
BThe method continues executing the next statement after throw
CThe method terminates immediately and control transfers to the nearest catch block
DThe method restarts from the beginning
Attempts:
2 left
πŸ’‘ Hint
Think about what throw does to the normal flow of a program.
❓ Predict Output
expert
3: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());
        }
    }
}
ACaught NullPointerException
BCompilation error due to nested try-catch
CNo output, program runs silently
DInner exception
Attempts:
2 left
πŸ’‘ Hint
Which catch block matches the thrown exception type?