CommencerCommencer gratuitement

JSON validation

Your product analysis is working well, and the team wants to deploy it as an automated daily report. But in production, JSON files might be missing, corrupted, or arrive in unexpected formats. Before going live, you need to add error handling so the application fails gracefully.

The JsonReader, JsonReadOptions, and Table classes have been imported for you.

Cet exercice fait partie du cours

Importing Data in Java

Afficher le cours

Instructions

  • Add a try block to wrap the JSON loading code.
  • Add a catch block to handle any exceptions.
  • Print the error message if loading fails.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

public class JSONValidation {
    public static void main(String[] args) {
        // Add try block for JSON loading
        ____ {
            JsonReadOptions options = JsonReadOptions.builder("products.json").build();
            Table products = new JsonReader().read(options);
            
            System.out.println("Successfully loaded " + products.rowCount() + " products");
            System.out.println(products.first(3));
        // Add catch block for exceptions
        } ____ (Exception e) {
            // Print error message
            System.err.println("Error reading JSON: " + e.____());
        }
    }
}
Modifier et exécuter le code