Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

306
Visualizações
Why the put method of a JSONObject throws JSONException?

I found few answers here on how to handle this exception, but there was no explanation why would this happen in the first place. I have the following code:

for (Map.Entry<String, Double> entry: qosMap.entrySet()) {
        JSONObject qosEntry = new JSONObject();
        try {
            qosEntry.put(entry.getKey(), entry.getValue());
        } catch (JSONException ex) {
            Logger.getLogger(JSONUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
}

The qosMap will never be empty, and the data in this map will always be valid.

What would be a case where the exception would thrown? Why do I need to have this extra code?

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

check java docs

"If a value is a non-finite number or if a name is duplicated."

https://docs.oracle.com/middleware/maf222/mobile/api-ref/oracle/adfmf/json/JSONObject.html

about 4 years ago · Santiago Trujillo Relatório

0

Assuming that you are using JSONObject from org.json

org.json.JSONObject.put(String, double) : JSONObject

It puts a key/double pair in the JSONObject.

Parameters:

key: A key string. value: A double which is the value.

Returns: this

Throws:

JSONException If the key is null or if the number is invalid.

I know that the data is always valid. why do I need to have this extra code?

You could make the method throws the exception, as you don't like clutter up your code and also it seems you can't do anything useful by catching the exception either.

It is obvious that you know the data you passed is valid. However the library won't know. That's why it's throwing an checked exception just to let you know in case it fails.

about 4 years ago · Santiago Trujillo Relatório

0

For all types T, JSONObject.put(String key, T value) throws a JSONException when the key is null or the value is a non-finite #. For type double, a JSONException is also thrown if the double value is invalid. See:

https://docs.oracle.com/middleware/maf222/mobile/api-ref/oracle/adfmf/json/JSONObject.html https://developer.android.com/reference/org/json/JSONObject.html

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda