I have a method which needs to act based on response from an other service.
The expected response is {"status":"success"}
I have 2 methods in my java code.
String getResultParam( HttpUriRequest request ) {
JSONObject res = getResult( request );
return res.getString( "status");
}
JSONObject getResult( HttpUriRequest request ) {
String res = // process the request and get the response String
JSONObject jsonObj = new JSONObject(res);
}
The first is a generic method to execute any request and get the response string as a JSON. Here, new JSONObject(res)
throws a JSONException.
The second method needs to get the status
from the JSON after executing the request.Here, res.getString("status")
again throws a JSONException.
How should I handle these exceptions?
- Do I need to propogate the exeception as
throws JSONException
to all the methods using this method? - If I do need to propogate the exception to all the methods calling this method, where would be the point where I catch and handle the exception?
- Do I need to catch the exception in the same method and throw a runtime exception?
- Do I return
null
on the generic method?
What would be the proper design to handle this Exception? Any advice would be appreciated.