tags:

views:

67

answers:

2

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() + "]:"); I have 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. 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. 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.

+1  A: 

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.

Nathan Hughes
I'm subclassing it because it seems logical (I need almost the same behaviour, with a minor toString change). The reason I just don't change the original class is because it is a shared class and someone could be needing that format of output.
Andrija
A: 

The problem appears when, in a method, the original is thrown because the catch block set to 'NoValueForParametarWebServiceException' doesn't catch the original. 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).

And depending upon which exception is thrown its toString() method will be called. (Simple inheritance rule) i.e.

  • NoValueForParametarException is thrown toString defined in NoValueForParametarException class will be called for instance.
  • And if NoValueForParametarWebServiceException is thrown then overriden toString method from NoValueForParametarWebServiceException will be called.

Some tutorials related to exceptions:

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

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

Hope this helps.

YoK
Hmm, the thing is, I always need the NoValueForParametarWebServiceException toString() method to run because I return the result of the toString() method to a web service caller and it needs to be in a different format (} catch (NoValueForParametarException nvfpe) { result = nvfpe.toString(); })
Andrija
@Andrija if NoValueForParametarWebServiceException is thrown its toString() will be called even if you catch NoValueForParametarException
YoK
I understand, but that's not quite the thing I need, I'll post and Edit to further clarify my problem.
Andrija