dcsimg
How can I throw an Exception of another type from ...
19 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 
Results  11 to 19 of 19  « Prev 1 2 Next » 

Posted By:   Ulrich_Winter
Posted On:   Monday, February 5, 2001 05:54 AM

How can I throw an Exception of another type from within a catch block but still keep the information of the original, caught exception?

Re: How can I throw an Exception of another type from...

Posted By:   Luigi_Viggiano  
Posted On:   Wednesday, February 21, 2001 01:10 AM

You can take example from java.rmi.RemoteException. This exception wraps inside the original exception, and when you throw it you must code as follow

try {
....
} catch (MyAppException myEx) {
throw new RemoteException("message", myEx);
}

Generally is not considered a good practice to extend the RemoteException, but you can copy the code from it and make your custom exception that wraps the original exception. Anoter good thing of the RemoteException is that it's thread safe. I think it's the standard way to meet your requirements.

To check the code of java.rmi.RemoteException, you need sources of the runtime libraries of the jdk, you may have already (i.e. if you use Jbuilder) or you have to download them from Sun's site.

Best regards,
Luigi.

Re: How can I throw an Exception of another type from...

Posted By:   Ulrich_Winter  
Posted On:   Tuesday, February 20, 2001 10:15 AM

I found no general way to save the complete information of the caught exception.
As a minimum/trivial solution one could use the string representation of
the caught exception as part of the message string of the new exception.


java.sql.SQLException supports chaining of exceptions by its methods get/setNextException().


JSR059 mentions the general support for exception chaining for release 1.4 of J2SE ( called Merlin) as a "Target of Opportunity".


This is described in JSR059 public draft below ID4293532.

Re: How can I throw an Exception of another type from...

Posted By:   Bill_Siggelkow  
Posted On:   Friday, February 16, 2001 10:42 AM

A typical way of doing this is to provide a wrapper exception (similar to RemoteException) that encapsulates the root or "caused by" exception. The encapsulating exception can have a method to get the caused by exception and may also override printStackTrace to print the stack trace of the encapsulated exception.

Re: How can I throw an Exception of another type from...

Posted By:   Anonymous  
Posted On:   Friday, February 16, 2001 07:22 AM

Just append the first exception's description after the second exception's description in the second exception's contructor:
for Example:
try {
throw new firstException("message");
}catch(firstException fe)
throw new secondException("message;"+fe.toString());

Re: How can I throw an Exception of another type from...

Posted By:   Francois_Nadeau  
Posted On:   Friday, February 16, 2001 06:27 AM

This is a quick and dirty way of getting that problem solved:



Inside the method which will be throwing the exception use this code :

try {
//Dance a little.
}catch( Exception e ) {
this.lastException = e ;
throw new XException() ;
}


Also inside the class which has this code inside of it, you will need an Exception variable and a method to retrieve it, such as these :

private Exception lastException = null ;

public Exception getLastException() {
return lastException ;
}


This will throw an exception of your choice and keeps the exception which was really triggered inside the class.

Re: How can I throw an Exception of another type from...

Posted By:   Ravi_Van  
Posted On:   Wednesday, February 14, 2001 02:45 PM

This is one way of doing it. Folks, any better solutions?
-Ravi.

try{

}catch(MyException e){

throw new MyNewException(e.toString());

}

Re: How can I throw an Exception of another type from...

Posted By:   Chandra_Patni  
Posted On:   Saturday, February 10, 2001 02:08 AM

The exception thrown in a catch block can keep the original exception by simply encapsulating the original exception in a member variable of Throwable type. This is also referred to as nested exceptions or chained exceptions. java.rmi.RemoteException and java.sql.SQLException are examples of such nested/chained exceptions.
The following snippet of code demonstrates the usage.


....
try {
try {
throw new OriginalException("...and all our woe.");
}
catch(OriginalException oe) {
throw new NestedException("Nested Exception", oe);
}
}
catch(NestedException ex) {
Throwable t = ex.getOriginalException(); // to get the orginal exception
}
....

public class NestedException extends Exception {
private Throwable originalException; // Placeholder for original exception

// contract of Throwable classes for its subclasses
public NestedException() { }
public NestedException(String message) { super(message); }

//Constructor to carry the nested exception
public NestedException(String message, Throwable originalException) {
super(message);
this.originalException = originalException;
}

public Throwable getOriginalException() {
return originalException;
}

// ........ Rest of the overridden methods if required
}

Re: How can I throw an Exception of another type from...

Posted By:   Joe_Morse  
Posted On:   Friday, February 9, 2001 10:46 AM

One clarification: your intent is to catch one type of exception, then throw another type of exception? If so, the implementation is fairly straightforward:

try {

  ...

} catch( BadException e ) {

 throw new SpecialException( e.getMessage() );

}


Be sure your method is declared to throw your SpecialException (whatever it may be). You may also want to do a printStackTrace() on the original exception, just to indicate that it originated in your try block.

Re: How can I throw an Exception of another type from...

Posted By:   Anonymous  
Posted On:   Friday, February 9, 2001 04:47 AM

Create an exception class of your own that holds the original exception.


public class WrapperException extends Exception {

private Exception original;

public WrapperException(Exception original) {
this.original = original;
}

public Exception getOriginal() {
return original;
}

}

Then throw one of these in the catch block, wrapping the original exception


...
} catch (Exception ex) {
throw new WrapperException(ex);
}
Results  11 to 19 of 19  « Prev 1 2 Next » 
About | Sitemap | Contact