Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9120053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:32:35+00:00 2026-06-17T05:32:35+00:00

I am using JSONObject to build a JSON response. The problem I’m facing is

  • 0

I am using JSONObject to build a JSON response. The problem I’m facing is how to handle the exception that .put() throws. I surrounded my code with a try catch, but I want to output JSON in my catch as well. I’ve been doing this by hand as seen below, but this seems prone to errors.What is the right way to handle this exception?

try{
    myResponse.put("successful",true);
    resp.getOutputStream().print(myeResponse.toString());
} catch (JSONException e) {
    resp.getOutputStream().print("{\"successful\":false, \"error\":\"Changes could not be saved.  Please reload the page and try again.\"}");
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:32:36+00:00Added an answer on June 17, 2026 at 5:32 am

    What you need to figure out is when the put() method of JSONObject would throw JSONException.

    From the Javadoc

    public JSONObject put(java.lang.String key,
                          boolean value)
                   throws JSONException
    
    Put a key/boolean pair in the JSONObject.
    
    Parameters:
        key - A key string.
        value - A boolean which is the value. 
    Returns:
        this. 
    Throws:
        JSONException - If the key is null.
    

    So, all you need to take care is that your key is not null which would surely be the case because your key is "successful".

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using this code for detect the source language.. jsonObj = new JSONObject(response);
I'm really new to using JSON to handle my Ajax Request and Response cycle.
I want to build nested json object using python jsonpickle, something like this {key:
I'm checking if an object in a JSON string exists using this: JSONObject json
I have a working JavaScript code below which dynamically creates JSON object using JSON.parse
I have updated my jSon object using the code <script type=text/javascript > jQuery(document).ready(function($){ var
I have just built a custom autocomplete component using jquery. My problem is that
I am using this simple code from here: https://developers.facebook.com/docs/mobile/android/build/#sig public class MainActivity extends Activity
Json String: (response) {19:{id:19,name:Product,sku:123,price:59.50,cost:25.00},20:{id:20,name:Test,sku:456,price:50.00,cost:40.00}} JavaScript Code: var json = $.parseJSON(response); var items = new
I'm using json.org JSONObject. I've tried to serialize it to memcache and got the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.