【发布时间】:2013-05-29 17:30:45
【问题描述】:
我需要在 Java 中创建一个异常,该异常的运行方式与以下 C# 异常相同。
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
【问题讨论】: