【问题标题】:How can I customize custom exceptions in Java?如何在 Java 中自定义自定义异常?
【发布时间】:2010-08-17 17:21:04
【问题描述】:

这是原始异常代码

public class NoValueForParametarException extends Exception {

private Exception nestedException;
 private int errorCode;

 public NoValueForParametarException(String message) {
  super(message);
 }

 public NoValueForParametarException(Exception ex,String message) {
  super(message);
  this.nestedException = ex;
 }

 public NoValueForParametarException(String message, int errorCode) {
  super(message);
  this.setErrorCode(errorCode);
 }

 public Exception getNestedException() {
  return this.nestedException;
 }

 public void setNestedException(Exception nestedException) {
  this.nestedException = nestedException;
 }

 public int getErrorCode() {
  return this.errorCode;
 }

 public void setErrorCode(int errorCode) {
  this.errorCode = errorCode;
 }

 public String toString() {
  StringBuffer errorMsg = new StringBuffer();
  errorMsg.append("[" + super.getMessage() + "]:");
  errorMsg.append((this.nestedException != null) ? ("\n[Nested exception]:" +   this.nestedException):"");
  return errorMsg.toString();
 }
}

这是新的

public class NoValueForParametarWebServiceException extends NoValueForParametarException {

 public NoValueForParametarWebServiceException(String message) {
  super(message);
 }

 public NoValueForParametarWebServiceException(Exception ex,String message) {
  super(message);
  this.setNestedException(ex);
 }

 public NoValueForParametarWebServiceException(String message, int errorCode) {
  super(message);
  this.setErrorCode(errorCode);
 }

 public String toString() {
  StringBuffer errorMsg = new StringBuffer();
  errorMsg.append(super.getMessage());
  errorMsg.append((this.getNestedException() != null) ?   ("\n[Nested     exception]:" + this.getNestedException()):"");  
  return errorMsg.toString();
 }
}

我所需要的只是更改toString() 方法的一部分,所以我有errorMsg.append(super.getMessage()); 而不是errorMsg.append("[" + super.getMessage() + "]:");。当在一个方法中由于设置为NoValueForParametarWebServiceException 的catch 块没有捕捉到原始数据而抛出原始数据时,就会出现问题。我知道我可以抓住原来的,然后重新扔新的(这也很令人满意),但我想知道是否还有其他方法。

编辑: 看来我需要什么不清楚,所以要更清楚: 程序抛出NoValueForParametarException。我想抓住它但使用NoValueForParametarWebServiceExceptiontoString() 方法(这是创建新类的唯一原因),因为我需要新版本的输出格式而不改变旧版本。

【问题讨论】:

    标签: java exception


    【解决方案1】:

    我认为没有任何理由对您的第一个异常进行子类化。此外,如果您摆脱了nestedException 实例变量并改用java.lang.Throwable 的原因,您就不必搞乱覆盖toString 并且可以删除大部分代码。

    【讨论】:

    • 我对它进行子类化,因为它看起来合乎逻辑(我需要几乎相同的行为,只是对 toString 进行了微小的更改)。我只是不更改原始类的原因是因为它是一个共享类,并且有人可能需要这种格式的输出。
    【解决方案2】:

    问题出现在方法中, 原件被抛出,因为 捕获块设置为 'NoValueForParametarWebServiceException' 没有抓住原件。我知道我 可以赶上原来的,只是 重新扔一个新的(这也将 满足)

    您不需要重新抛出子异常。 在这种情况下,只需捕获父异常。

    try{
      //your code
    }catch(NoValueForParametarException e){
      //whatever handling you need to do. suppose you call toString() method
      System.out.println(e.toString());
    }
    

    在上述情况下,如果抛出任何异常,将执行 catch 块 (NoValueForParametarException 或 NoValueForParametarWebServiceException)。

    根据抛出的异常,将调用其 toString() 方法。 (简单继承规则)即

    • NoValueForParametarException 是 抛出 toString 定义在 NoValueForParametarException 类 例如将被调用。
    • 如果 NoValueForParametarWebServiceException 被抛出然后覆盖 toString 方法从 NoValueForParametarWebServiceException 将被调用。

    一些与异常相关的教程:

    http://download.oracle.com/javase/tutorial/essential/exceptions/index.html

    http://tutorials.jenkov.com/java-exception-handling/index.html

    希望这会有所帮助。

    【讨论】:

    • 嗯,问题是,我总是需要运行 NoValueForParametarWebServiceException toString() 方法,因为我将 toString() 方法的结果返回给 Web 服务调用者,并且它需要采用不同的格式( } 捕捉 (NoValueForParametarException nvfpe) { 结果 = nvfpe.toString(); })
    • @Andrija 如果 NoValueForParametarWebServiceException 被抛出,即使你捕捉到 NoValueForParametarException 也会调用它的 toString()
    • 我明白,但这不是我需要的东西,我会发布并编辑以进一步澄清我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 2018-07-27
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多