Re: How can I throw an Exception of another type from ...
Posted By:
Anonymous
Posted On:
Wednesday, March 4, 2009 05:30 AM
just catch the exception and create an object of a desired excepton class and then pass it by throw to the appropriate catch
Re: How can I throw an Exception of another type from ...
Posted By:
Ratnakar_Sadasyula
Posted On:
Wednesday, March 26, 2008 08:46 PM
Ulrich there are two ways you could handle this
1) In the catch block, print the information of the exception and then throw the Exception of another type
eg:
try
{
System.out.println("Ex1");
//body
}
catch( Exception ie)
{
System.out.println("IQ");
throw new MyException(me);
}
2) Use a finally block to retain information of original caught exception
eg:
try
{
System.out.println("Ex1");
//body
}
catch( Exception ie)
{
System.out.println("IQ");
throw new MyException(me);
}
finally
{
System.out.println(" Info of Original Ex");
}
Re: How can I throw an Exception of another type from...
Posted By:
Jim_Harte
Posted On:
Monday, March 12, 2001 09:50 AM
If you have control over the exception which is to be thrown, that is to say you have written the exception class yourself, then you can have your exception class have an attribute which will hold the original exception.
Otherwise, you can always serialize the exception and store it away on disk, transmit it to another class, or store it in some type of cache to be accessed later.
Re: How can I throw an Exception of another type from...
Posted By:
Tom_Kerigan
Posted On:
Wednesday, February 21, 2001 11:32 AM
I can think of two possible solutions, both equally hacky, but given the state of the specification, it doesn't seem like there's any really clean way to do what you wish.
(1) Any time you throw a new exception, from a catch block, run the printStackTrace(pw PrintWriter) on the original caught exception. As your PrintWriter instance's constructor parameter, use a CharArrayWriter. Using the toString() method on the CharArrayWriter, you now have a String representation of the stack trace up until the time that you caught it. Now, you can either stuff the String into the message portion of the new exception that you are about to throw, or you can subclass the exception that you throw, and include a String instance variable to store this. At any point that the exception is caught and rethrown, you can continue to append the stack traces until you get to a point where you want to view them, ie. log them or what have you. At this point, you simply take the stored stack trace, as well as the current stack trace (ie. from printStackTrace(...)) and after appending them, output them in a desirable fashion.
(2) Extend any exception that you throw, and include a Vector instance variable. The idea here is to maintain a vector of Exceptions, so that later we can individually run printStackTrace(...) on them. Any catch block that will rethrow an exception (where you wish to maintain a handle on the entire stack trace) will simply append the caught exceptions that exist in the caught exception's vector to the vector that exists in the new exception to be thrown. At the same time, we also must append the current exception that we just caught, so that the last set of stack frames are preserved. The developer may wish to null the Vector instance before this appending, since we already have a copy of all of the previous exceptions.