简体   繁体   English

如何在Java中自定义自定义异常?

[英]How can I customize custom exceptions in Java?

This is the original exception code 这是原始的异常代码

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

and this is the new one 这是新的

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

All I need is to change the part of the toString() method so instead of errorMsg.append("[" + super.getMessage() + "]:"); 我需要做的只是更改toString()方法的一部分,而不是errorMsg.append("[" + super.getMessage() + "]:"); I have errorMsg.append(super.getMessage()); 我有errorMsg.append(super.getMessage()); . The problem appears when, in a method, the original is thrown because the catch block set to NoValueForParametarWebServiceException doesn't catch the original. 当在一种方法中,由于设置为NoValueForParametarWebServiceException的catch块未捕获原始数据而引发原始数据时,会出现问题。 I know I could catch the original and just re-throw the new one (which would also be satisfying), but I was wondering if there is another way. 我知道我可以捕捉到原始图像,然后重新抛出新的图像(这也会令人满意),但是我想知道是否还有另一种方法。

EDIT: It seems what I need is unclear, so to be more clear: The program throws NoValueForParametarException . 编辑:似乎我需要不清楚,所以要更清楚:该程序引发NoValueForParametarException I want to catch it but use the toString() method of NoValueForParametarWebServiceException (that is the sole reason of creating the new class) because I need the output format of the new version without changing the old. 我想捕获它,但是使用NoValueForParametarWebServiceExceptiontoString()方法(这是创建新类的唯一原因),因为我需要新版本的输出格式而不更改旧版本。

I don't see any reason to subclass your first exception. 我认为没有任何理由可以继承您的第一个例外。 Also, if you get rid of the nestedException instance variable and use java.lang.Throwable's cause instead, you don't have to mess with overriding toString and you can delete most of this code. 另外,如果您摆脱了nestedException实例变量,而改用java.lang.Throwable的原因,则不必搞乱重写toString,并且可以删除大部分代码。

The problem appears when, in a method, the original is thrown because the catch block set to 'NoValueForParametarWebServiceException' doesn't catch the original. 当在一种方法中,由于设置为'NoValueForParametarWebServiceException'的catch块未捕获原始数据而引发原始数据时,会出现问题。 I know I could catch the original and just re-throw the new one (which would also be satisfying) 我知道我可以抓到原来的,然后重新扔掉新的(这也会令人满意)

You don't need to re throw child exception. 您不需要重新引发子异常。 Just catch parent exception in this case. 在这种情况下,只需捕获父异常即可。

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

In above case catch block will be executed if any of your exceptions are thrown (NoValueForParametarException or NoValueForParametarWebServiceException). 在上述情况下,如果抛出任何异常(NoValueForParametarException或NoValueForParametarWebServiceException),则将执行catch块。

And depending upon which exception is thrown its toString() method will be called. 并且根据抛出的异常,将调用其toString()方法。 (Simple inheritance rule) ie (简单的继承规则)即

  • NoValueForParametarException is thrown toString defined in NoValueForParametarException class will be called for instance. 例如,将调用NoValueForParametarException抛出的NoValueForParametarException类中定义的String。
  • And if NoValueForParametarWebServiceException is thrown then overriden toString method from NoValueForParametarWebServiceException will be called. 如果抛出NoValueForParametarWebServiceException,则将调用从NoValueForParametarWebServiceException重写的toString方法。

Some tutorials related to exceptions: 一些与异常有关的教程:

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

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

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM