2010-08-17 76 views
1

这是原来的异常代码如何在Java中定制自定义异常?

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());。在某个方法中,由于catch块设置为NoValueForParametarWebServiceException未捕获原始文件,所以会抛出原始文件。我知道我可以抓到原来的,只是重新投掷新的(这也会令人满意),但我想知道是否有另一种方式。

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

回答

1

我没有看到任何理由继承你的第一个异常。另外,如果你摆脱了nestedException实例变量,而是使用java.lang.Throwable的原因,则不必混淆覆盖toString,并且可以删除大部分代码。

+0

我是继承它,因为它似乎是合乎逻辑的(我需要几乎相同的行为,与一个小的toString更改)。我只是不改变原始类的原因是因为它是一个共享的类,有人可能需要这种输出格式。 – Andrija 2010-08-17 17:50:59

0

问题出现时,在一个方法中, 原始抛出因为 catch块设置为 “NoValueForParametarWebServiceException” 不捕获原稿。我知道我 能赶上原来的,只是 再次引发新的(也 将是令人满意的)

你并不需要重新抛出异常的孩子。 在这种情况下,赶上父母的异常。如果您的任何异常的抛出 (NoValueForParametarException或NoValueForParametarWebServiceException)

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

在上述情况下catch块将被执行。

取决于抛出了哪个异常,它的toString()方法将被调用。 (简单继承规则),即

  • NoValueForParametarException在 NoValueForParametarException类 定义 抛出的toString将被称为例如。
  • 如果 NoValueForParametarWebServiceException 从 NoValueForParametarWebServiceException 抛出,则重写的toString方法 将被调用。

有关的异常有些教程:

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

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

希望这有助于。

+0

嗯,事情是,我总是需要NoValueForParametarWebServiceException toString()方法来运行,因为我将toString()方法的结果返回给Web服务调用方,并且它需要使用不同的格式(catch)(NoValueForParametarException nvfpe ){ \t \t \t \t结果= nvfpe.toString(); \t \t \t}) – Andrija 2010-08-17 18:25:35

+0

@Andrija如果NoValueForParametarWebServiceException抛出其toString()将被即使你抓住NoValueForParametarException – YoK 2010-08-18 12:02:23

+0

我理解所谓的,但事实并非完全如此我需要,我会发布和编辑,以进一步澄清我的问题。 – Andrija 2010-08-18 17:34:46