0
0
Javaprogramming~20 mins

Throws keyword in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Throws 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 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());
        }
    }
}
ACompilation error: unhandled exception
BException in thread "main" java.io.IOException: File error
CCaught: File error
DNo output
Attempts:
2 left
πŸ’‘ Hint
Look at how the exception thrown by readFile() is handled in main.
❓ Predict Output
intermediate
2: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");
    }
}
ACompilation error: unhandled exception
BPrints 'After risky call'
CRuntime exception thrown, program crashes
DNo output
Attempts:
2 left
πŸ’‘ Hint
Check if main handles or declares the exception thrown by risky().
πŸ”§ Debug
advanced
2: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();
    }
}
ACompilation error: unhandled IOException in caller()
BCompilation error: method() must not declare throws IOException
CRuntime error: IOException thrown but not caught
DNo error, compiles fine
Attempts:
2 left
πŸ’‘ Hint
Check if caller() handles or declares the exception thrown by method().
🧠 Conceptual
advanced
1:30remaining
Which statement about throws keyword is true?
Select the correct statement about the throws keyword in Java.
Athrows keyword is used to handle exceptions inside a method.
Bthrows keyword catches exceptions thrown by other methods.
Cthrows keyword can only be used with RuntimeExceptions.
Dthrows keyword declares that a method might throw exceptions to its caller.
Attempts:
2 left
πŸ’‘ Hint
Think about what throws does in a method signature.
❓ Predict Output
expert
3: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());
        }
    }
}
A
Caught in outer: Inner exception
Caught in main: Inner exception
B
Caught in outer: Inner exception
Finally block executed
Caught in main: Inner exception
C
Finally block executed
Caught in main: Inner exception
DCaught in main: Inner exception
Attempts:
2 left
πŸ’‘ Hint
Remember that finally block always runs, and exceptions rethrown propagate up.