Java – What is a good practice for handling some runtime HTTP exceptions?

What is a good practice for handling some runtime HTTP exceptions?… here is a solution to the problem.

What is a good practice for handling some runtime HTTP exceptions?

I have a little method that looks like this :

public static void unstarTrack(Context ctxContext, String strId) {

try {

HttpParams htpParameters = new BasicHttpParams();

List<NameValuePair> lstCredentials = new ArrayList<NameValuePair>();
      lstCredentials.add(new BasicNameValuePair("t", String.valueOf(System.currentTimeMillis() / 1000)));
      lstCredentials.add(new BasicNameValuePair("__call", "favourites.removeSong"));

HttpPost htpPost = new HttpPost(API_URL);
      htpPost.setEntity(new UrlEncodedFormEntity(lstCredentials));
      htpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:18.0) Gecko/20100101 Firefox/18.0");
      htpPost.addHeader("Accept-Encoding", "gzip");
      DefaultHttpClient dhcClient = new DefaultHttpClient(htpParameters);

HttpResponse resResponse = dhcClient.execute(htpPost);
      Log.d(TAG, EntityUtils.toString(resResponse.getEntity()));

return;

} catch (SocketException e) {
    throw new RuntimeException("problem with network connectivity.", e);
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException("Encoding not supported.", e);
} catch (ClientProtocolException e) {
    throw new RuntimeException("A protocol exception was encountered.", e);
} catch (ParseException e) {
    throw new RuntimeException("An error occurred while trying to read the header elements.", e);
} catch (IOException e) {
    throw new RuntimeException("An error occurred while trying to read response stream.", e);
}

The method itself is very simple, but it has a bunch of exceptions that occur and I don’t know how I should handle them. Suppressing them with a simple “e.printStackTrace()” didn’t seem like a good idea, so I started reading exception handling best practices, but I’m still a bit lost. How should I handle exceptions?

I

need to do some handling with exceptions because I don’t want to return null from the method. Returning a null from my method means that the calling method will not know if an exception has occurred in my method.

Should I create a custom exception and throw it or should I simply throw an unchecked exception?

Calling the method

doesn’t really affect my method, i.e. if there’s a problem with the network connection, SocketException can happen, and IOException can happen if there’s a problem with the read stream. Invoking the method can only be retried later at most.

If I rethrow all the exceptions I caught, calling the method will just be full of exception-handling blocks.

(I’m sorry if this seems like a trivial question.) I just want to learn to write better code. Thank you. )

Solution

Create a private exception (similar to UnstarTrackException) with an appropriate level of abstraction. Throw such an exception that wraps the original exception you caught. This way, the caller only needs to handle one exception (I assume that all exceptions should be handled the same way: retry).

Whether the abnormality should be checked depends on your taste. If you want to force all callers of the method to handle this exception, make it checked for exceptions. If you want the caller to choose whether he wants to handle this exception, use a runtime exception.

If this method is buried deep in the code layer and exceptions can only be handled at the top level, then runtime exceptions may be a better choice. In fact, unless you are the only caller of this method, a run-time exception might also be a better choice. Today, checked anomalies are often underused.

Related Problems and Solutions