我需要在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
[http://docs.oracle.com/javase/tutorial/essential/exceptions/](http://docs.oracle.com/javase/tutorial/essential/exceptions/) – jlordo