Handling Exceptions in JSON Parsing Example

This program demonstrates handling exceptions in JSON parsing.

Problem Statement

Write a Java program that handles exceptions during JSON parsing.

Source Code

1 import org.json.*;
2 public class JSONParsingExceptionExample {
3 public static void main(String[] args) {
4 System.out.println("Eduinq JSON Parsing Exception Example");
5 String json="{\"name\":\"Eduinq\",\"age\":\"abc\"}";
6 try {
7 JSONObject obj=new JSONObject(json);
8 int age=obj.getInt("age");
9 System.out.println("Eduinq Age: "+age);
10 } catch(JSONException e) {
11 System.out.println("Eduinq Exception: Invalid JSON Data");
12 }
13 }
14 }

Program Output

					Eduinq JSON Parsing Exception Example
Eduinq Exception: Invalid JSON Data
				

Explanation

JSON parsing may throw exceptions if data types are invalid. - Try-catch ensures program continues gracefully. - Provides meaningful error messages for debugging. - In enterprise systems, JSON exception handling ensures reliable data exchange. Conclusion: Shows handling exceptions in JSON parsing.

Quick Links to Explore