Pergunta

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);
    }
}
Foi útil?

Solução

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.
}

Outras dicas

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;
    }

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;
    }

}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top