2013-05-29 85 views
0

我需要在Java中創建一個異常,其運行方式與以下C#類似。創建一個類似於C#異常的Java異常

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

目前我找不到一個以這種方式工作的Java異常的例子。唯一的例外上述被稱爲以這種方式

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

我通過以下線程讀取,但他們沒有提供我怎麼會去這麼多的洞察力,或者如果它甚至有可能。

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

+0

[http://docs.oracle.com/javase/tutorial/essential/exceptions/](http://docs.oracle.com/javase/tutorial/essential/exceptions/) – jlordo

回答

2

你總是可以創建自定義異常,你只需要hadle他們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; 
} 

}

和u SE

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

我建議如下:

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

只是缺少getter和setter在這個例子。