简体   繁体   中英

Creating a Java Exception that is similar to a C# Exception

I need to create an exception in Java that operates in the same manner as the following C# one.

public class ResponseException : Exception
{
    internal ResponseException(int statusCode, String StatusCodeDescription, String request, String response)
        : base("Returned an error status code " + statusCode.ToString() + " (" + StatusCodeDescription + ") " + response)
    {
        this.StatusCode = statusCode;
        this.StatusCodeDescription = StatusCodeDescription;
        this.Request = request;
        this.Response = response;
    }

    public int StatusCode { get; set; }
    public String StatusCodeDescription { get; set; }
    public String Request { get; set; }
    public String Response { get; set; }
}

Currently I can not find an example of a Java exception that works in such a way. The exception above is called in this manner

throw new ResponseException(((int)response[1]), 
   ((String)response[2]), url, ((String)response[0]));

I read through the following threads but they didn't provide much insight on how I would go about this, or if it is even possible.

How to create a custom exception type in Java?

How to create custom exceptions in Java?

http://www.java-forums.org/java-lang/7699-how-create-your-own-exception-class.html

You can always create your custom exception and you only need to hadle them diiferently

class MyException extends Exception {

public int StatusCode;
public String StatusCodeDescription;
public String Request;
public String Response;

public MyException() {
}

public MyException(String msg) {
    super(msg);
}

public MyException(int statusCode, String statusCodeDescription,
        String request, String response) {
    StatusCode = statusCode;
    StatusCodeDescription = statusCodeDescription;
    Request = request;
    Response = response;
}

public int getStatusCode() {
    return StatusCode;
}

public void setStatusCode(int statusCode) {
    StatusCode = statusCode;
}

public String getStatusCodeDescription() {
    return StatusCodeDescription;
}

public void setStatusCodeDescription(String statusCodeDescription) {
    StatusCodeDescription = statusCodeDescription;
}

public String getRequest() {
    return Request;
}

public void setRequest(String request) {
    Request = request;
}

public String getResponse() {
    return Response;
}

public void setResponse(String response) {
    Response = response;
}

}

and use

throw new MyException(((int)response[1]), ((String)response[2]), url, ((String)response[0]));

I would suggest the following:

public class ResponseException extends Exception {

   private int statusCode;
   private String statusCodeDescription;
   private String request;
   private String response;

   public ResponseException(int statusCode, String statusCodeDescription, String request, String response) {
      super("Returned an error status code " + statusCode + " (" + statusCodeDescription + ") " + response);
      this.statusCode = statusCode;
      this.statusCodeDescription = statusCodeDescription;
      this.request = request;
      this.response = response;
   }
}

Just missing Getter and Setter in this example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM