星期四, 1月 12, 2012

[Java] 自訂Exception

簡單的自訂例外處理
public class HttpClientException extends Exception{
 
 private String protocol = null;
 private String endpoint = null;
 private String httpMethod = null;
 private String postParameters = null;
 private int statuscode = 0;
 private String msg = null;
 
 public HttpClientException(String msg){
  super(msg);
 }
 
 public HttpClientException(String endpoint,String httpMethod,String postParameters,int statuscode,String msg){
  this.endpoint = endpoint;
  //detect protocal from endpoint
  if(this.endpoint.contains("https"))
   this.protocol = "HTTPS";
  else
   this.protocol = "HTTP";
  
  this.httpMethod = httpMethod;
  this.postParameters = postParameters;
  this.statuscode = statuscode;
  this.msg = msg;
 }
 
 public String getEndPoint(){
  return this.protocol + " Request Endpoint:" + this.endpoint;
 }
 
 public String getHttpMethod(){
  return this.protocol +  " Request Method:" + this.httpMethod;
 }
 
 public String getRequestBody(){
  if(this.postParameters != null)
   return this.protocol + " Request Body:" + this.postParameters;
  else
   return "Can't get request body.";
 }
 
 public String getStatusCode(){
  return this.protocol + " Status Code:" + this.statuscode;
 }
 
 public String getErrorMessage(){
  return this.protocol + " Error Message:" + this.msg;
 }
}

沒有留言:

張貼留言

留個話吧:)