BaşlayınÜcretsiz Başlayın

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.

Bu egzersiz

Importing Data in Java

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • 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.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

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.____());
        }
    }
}
Kodu Düzenle ve Çalıştır