1

have a class that handles Http requests for me. This class contains some methods like

public static String getServerResponseByHttpGet(String url){...}
public static String getServerResponseByHttpGet(String url, String token){...}
public static String getServerResponseByHttpPost(String url, token, List<NameValuePair> pairs){...}

I'm able to get connection code by using httpResponse.getStatusLine().getStatusCode();

Since I need to handle different situations like if connection code is 401, 403, 04, 409, etc. then I want to throw this code and catch it in implementation class. Is it possible to do that. I have following class but have no idea how to modify it in order to send integer value.

public class MyHttpClientException extends Exception {

    public MyHttpClientException() {

    }

    public MyHttpClientException(String message) {
        super(message);
    }

    public MyHttpClientException (Throwable cause) {
        super (cause);
    }

    public MyHttpClientException (String message, Throwable cause) {
        super (message, cause);
    }
}
1
  • put the error code into your session Commented May 15, 2014 at 8:03

3 Answers 3

2

Exceptions are like other classes, you can add whatever you want in the implementation.

So to really simplify things:

public class MyHttpClientException extends Exception {
    private final int errorCode;

    public MyHttpClientException(final int errorCode) {
         this.errorCode = errorCode;
    }

    // rest of the constructors, one example:
    public MyHttpClientException(int errorCode, String message) {
         super(message);
         this.errorCode = errorCode;
    }

    public int getErrorCode{
        return errorCode;
    }
}

Then where the error happens

throw new MyHttpClientException(errorCodeHere);

When you catch it:

try {
    // request
} catch (MyHttpClientException e) {
    int errorCode = e.getErrorCode();
    // do whatever you want with it.
}
Sign up to request clarification or add additional context in comments.

Comments

1

Just like the Java RESTful service API's WebApplicationException, you can add a int member to your MyHttpClientException and set it on the constructor:

public class MyHttpClientException extends Exception {

    private int code;

    public MyHttpClientException(int code) {
        this.code = code;
    }
.
.
.

    public int getCode(){
        return code;
    }

Comments

1

When programmers get into touch with exceptions the first time, they always extend Exception and create all those constructors that this super class has.

But exceptions are also full-fledged objects that should be used as other objects, too. So the easiest way to declare such an exception is:

public final class MyHttpClientException extends Exception {

    private int final httpCode;

    public MyHttpClientException (int httpCode) {
        super(String.valueOf(httpCode));
        this.httpCode = httpCode;
    }

    public MyHttpClientException (int httpCode, Throwable cause) {
        super(String.valueOf(httpCode), cause);
        this.httpCode = httpCode;
    }

    public int httpCode() {
        return httpCode;
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.