Bird
Raised Fist0
Javaprogramming~20 mins

Throws keyword 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
πŸŽ–οΈ
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.

Practice

(1/5)
1.

What is the main purpose of the throws keyword in Java?

easy
A. To declare that a method might throw certain checked exceptions
B. To catch exceptions inside a method
C. To create a new exception object
D. To stop the program immediately when an error occurs

Solution

  1. Step 1: Understand the role of throws

    The throws keyword is used in a method signature to declare that the method might throw certain checked exceptions.
  2. Step 2: Differentiate from other keywords

    It does not catch exceptions (that's try-catch), nor create exceptions or stop the program immediately.
  3. Final Answer:

    To declare that a method might throw certain checked exceptions -> Option A
  4. Quick Check:

    throws declares exceptions [OK]
Hint: Remember: throws declares, catch handles exceptions [OK]
Common Mistakes:
  • Confusing throws with catch
  • Thinking throws creates exceptions
  • Believing throws stops program immediately
2.

Which of the following is the correct way to declare a method that might throw an IOException?

public void readFile() _____ IOException { }
easy
A. thrown
B. throw
C. throws
D. throws new

Solution

  1. Step 1: Recall correct syntax for exception declaration

    In Java, the keyword to declare exceptions a method might throw is throws.
  2. Step 2: Check options for syntax correctness

    throw is used to actually throw an exception inside method body, not in declaration. thrown and throws new are invalid.
  3. Final Answer:

    throws -> Option C
  4. Quick Check:

    Method declaration uses throws [OK]
Hint: Method declarations use 'throws', not 'throw' [OK]
Common Mistakes:
  • Using 'throw' instead of 'throws' in method signature
  • Adding 'new' after throws
  • Using non-existent keywords like 'thrown'
3.

What will be the output of the following code?

import java.io.*;

public class Test {
    public static void risky() throws IOException {
        throw new IOException("Error happened");
    }
    public static void main(String[] args) {
        try {
            risky();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
medium
A. Error happened
B. Compilation error due to missing throws
C. No output
D. Runtime error without message

Solution

  1. Step 1: Analyze method throwing exception

    The method risky() declares it throws IOException and actually throws it with message "Error happened".
  2. Step 2: Check exception handling in main

    The main method calls risky() inside a try block and catches IOException, printing the exception message.
  3. Final Answer:

    Error happened -> Option A
  4. Quick Check:

    Exception caught and message printed [OK]
Hint: Thrown exceptions must be caught or declared [OK]
Common Mistakes:
  • Thinking throws causes compile error if caught
  • Expecting no output because exception thrown
  • Confusing throws with throw inside method body
4.

Identify the error in the following code snippet:

public void process() {
    riskyMethod() throws IOException;
}
medium
A. Incorrect use of throws keyword inside method body
B. Missing try-catch block around riskyMethod() call
C. Method process() should declare throws IOException
D. All of the above

Solution

  1. Step 1: Check syntax of throws usage

    The throws keyword cannot be used inside a method body; it belongs in the method signature.
  2. Step 2: Analyze exception handling requirements

    Calling riskyMethod() which throws IOException requires either a try-catch block or declaring throws IOException in process().
  3. Step 3: Combine all errors

    All these issues are present: wrong throws usage, missing try-catch, and missing throws declaration.
  4. Final Answer:

    All of the above -> Option D
  5. Quick Check:

    Throws only in signature + handle exceptions [OK]
Hint: Throws keyword only in method signature, not inside body [OK]
Common Mistakes:
  • Using throws inside method body
  • Not handling checked exceptions properly
  • Forgetting to declare throws in method signature
5.

You have a method readData() that calls two other methods: openFile() and parseFile(). Both can throw IOException. How should you declare readData() to properly handle exceptions?

hard
A. Do nothing, exceptions will be handled automatically
B. Declare readData() with throws IOException and let caller handle it
C. Declare readData() with throws Exception to cover all exceptions
D. Use try-catch inside readData() to catch and ignore exceptions

Solution

  1. Step 1: Understand exception propagation

    If openFile() and parseFile() throw IOException, readData() must either handle or declare these exceptions.
  2. Step 2: Choose proper declaration

    Declaring throws IOException in readData() lets the caller decide how to handle exceptions, keeping code clean and clear.
  3. Step 3: Evaluate other options

    Ignoring exceptions is bad practice. Declaring throws Exception is too broad. Exceptions are not handled automatically.
  4. Final Answer:

    Declare readData() with throws IOException and let caller handle it -> Option B
  5. Quick Check:

    Declare checked exceptions to propagate [OK]
Hint: Declare throws for checked exceptions to pass responsibility [OK]
Common Mistakes:
  • Ignoring exceptions instead of declaring or catching
  • Declaring too broad exceptions like Exception
  • Assuming exceptions are handled automatically