Trying to convert String to JSONObject skips try & catch

时间:2017-01-03 10:22:40

标签: android

I am trying to convert string into JSONObject, but my application skip the try and catch. Here's the code :

if(!getUserRegisterInfo().equalsIgnoreCase(""))
{
    retrieveState();
}

retrieveState function:

private void retrieveState(){
    try {
        JSONObject obj = new JSONObject(UserPreferences.getUserRegisterInfo());
        JSONObject row = obj.getJSONObject("UserRegistrationInfo");

        Toast.makeText(getContext(),"firstName = " +row.getString("firstName") ,Toast.LENGTH_SHORT).show();
    } catch (Throwable t) {
        t.printStackTrace();
    }
}

It is not throwing any exception, nor Toast is shown, any help would be really valuable for me!

1 个答案:

答案 0 :(得分:0)

如果JsonObject抛出异常。那么你的代码永远不会执行剩下的try块。

试试这个

private void retrieveState()
{
    try
    {
        JSONObject obj = new JSONObject(UserPreferences.getUserRegisterInfo());
        JSONObject row = obj.getJSONObject("UserRegistrationInfo");

    } 
    catch (Throwable t) 
    {
        t.printStackTrace();
    }
    finally
    {
        Toast.makeText(getContext(),"Your Message" ,Toast.LENGTH_SHORT).show();
    }
}