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