How can I throw an Exception of another type from ...
19 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 
Results  1 to 10 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:   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:   Vadim_Gurov  
Posted On:   Sunday, March 11, 2001 11:32 PM

You have to use exception wrapper.


public class WrapperException extends Exception {

private Exception e;

WrapperException(Exception e) {
this.e = e;
}

public Exception getOriginalExeption() {
return e;
}
}

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

Posted By:   Iain_Ballard  
Posted On:   Wednesday, March 7, 2001 07:22 AM

The simple solution would be to assign a reference of the caught exception to a wider scope. i.e:


NullPointerException npe = null;
try {
} catch (NullPointerException e) {
npe = e;
}

Or, you could cast your specific exception to a type
Exception
and pass that along.

Finally, you could cast the caught exception to the new kind you want. I havn't tested this, so use caution:


IllegalAccessException iae;
try {
throw new NullPointerException("Info here");
} catch (NullPointerException npe) {
iae = (IllegalAccessException)((Exception)npe);
}

HTH.

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

Posted By:   Sandip_Chitale  
Posted On:   Thursday, March 1, 2001 10:13 AM

Use the nested exception strategy as found in


http://java.sun.com/products/jdk/1.2/docs/api/java/sql/SQLException.html


The following resource may be helpful also -


http://www.javaworld.com/javatips/jw-javatip91.html

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

Posted By:   Peter_Kofler  
Posted On:   Tuesday, February 27, 2001 12:42 AM

1) Most simple solution
throw new NewException(oldException.toString());

2) Create a Wrapper Exception which subclasses the NewException:

public class MyNewException extends NewException {

/** Old Information stored. */
OldException e;

public MyNewException(OldException e) {this.e = e;}

public OldException getInfo() { return e; }

}

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

Posted By:   Darren_Hobbs  
Posted On:   Monday, February 26, 2001 08:12 AM

You can call getMessage() on an exception object to get its message and then construct a new exception with the same message. If more information is needed, then you could call fillInStackTrace() first to add the stack trace to the exception object.

The code fragment to do that would look something like this:

try {
// some code that throws an exception
}

// simple re-throw
catch (simpleException se) {
throw new otherException(se.getMessage());
}

// more detailed information needed
catch (tracedException te) {
te.fillInStackTrace();
throw new otherException(te.getMessage());
}

Replacing simpleException, tracedException and otherException with whatever exception type you wish. The methods described are all defined within java.lang.Throwable, which every exception class extends.

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

Posted By:   Ryan_Breidenbach  
Posted On:   Wednesday, February 21, 2001 01:26 PM

There are two ways of doing this that are pretty straight forward:


  1. If all you really want to do is preserve the message of the exception, it is really easy. Your method would look something like:

    public void doSomething() throws IOException {
    try {
    // do something that may throw an SomeOtherException
    }
    catch (SomeOtherException e) {
    throw new IOException(e.getMessage());
    }
    }

    Of course, this only makes since if SomeOtherException somehow pertains to IO failure.



  2. Another alternative is to create your own excpetion class that is a wrapper around another exception, or even a collection of exceptions. This could come in handy for situations where you may need to release resources upon an exception, but releasing these resources can also throw an excpetion. This is very common in JDBC and file I/O.


    For example, take this code snippet:


    public Student getStudent(int id) throws SQLException {
    try {
    // JDBC code here...
    }
    catch(SQLException e) {
    // rethrow
    throws e;
    }
    finally {
    // always release Connection, but this can throw Exception!
    try {
    //release
    }
    catch(SQLException e) {
    // hide. BAD!!!!
    }
    }
    }

    If we throw the second exception, we lose the first one - the last exception thrown is the one that propogates up the thread. But, if we throw the original one, we lose the second one. What to do...


    Well, we can create another class, such as SQLWrapperException:


    public class SQLWrapperException {
    private java.util.List exceptions;

    public SQLWrapperException(java.util.ListList exeptions) {
    this.exceptions = exceptions;
    }

    public java.util.List getExceptions() {
    return exceptions;
    }
    }

    Now we have a structure that can collect all our excpetions. Now, the method would look like:

    public Student getStudent(int id) throws SQLWrapperException {
    List exceptions = new List();
    try {
    // JDBC code here...
    }
    catch(SQLException e) {
    exception.add(e);
    }
    finally {
    // always release Connection, but this can throw Exception!
    try {
    //release
    }
    catch(SQLException e) {
    exception.add(e);
    }
    if (exceptions.size() != 0) {
    throw new SQLWrapperException(exceptions);
    }
    }
    }

    While this is a pretty simplistic example, I think you get the picture. You can also create a very generic container, such as ExceptionWrapper or ExceptionListWrapper, that can hold any type of exception.

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.
Results  1 to 10 of 19  « Prev 1 2 Next » 
About | Sitemap | Contact