Aggregate Multiple Exceptions in Java: Explanation & Practice

Collect and report multiple validation errors

Problem summary

Validate multiple fields and collect all errors before reporting.

Starter code

import java.util.*;

public class Main {
    public static void main(String[] args) {
        String name = "";
        String email = "invalid";
        int age = -5;
        
        // Validate all fields
        // Collect all errors
        // Print: Errors: <list of errors>
    }
}

Expected output and test cases

  • Multiple errors
    Errors: [Name required, Invalid email, Age must be positive]
  • Single error
    Errors: [Invalid email]
  • All valid
    Valid

Hints

  1. Use List to collect errors
  2. Don't throw immediately
  3. Report all at once

Related Exception Handling exercises

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