Using Throws Clause in Java: Explanation & Practice

Declare exceptions in method signature

Problem summary

Create a method that declares it throws an exception, and handle it in main.

Starter code

public class Main {
    // Create method that throws IOException
    
    public static void main(String[] args) {
        // Call method and handle exception
    }
}

Expected output and test cases

  • Handle declared exception
    File operation failed

Hints

  1. Add 'throws ExceptionType' to method signature
  2. Caller must handle or propagate
  3. For checked exceptions

Related Exception Handling exercises

Practice all Exception Handling exercises · Run this idea in the Java compiler