Ali
Ali

Reputation: 2042

Throw nested exception through java Throwable

I trying to throw inner exception in another exception through java Throwable but IDE told my that you must surround it with try/cath, What should I do to avoid from this problem?

    try
    {
        //Some code
    }
    catch (IOException e) 
    {
        Throwable cause = new Throwable();
        cause.initCause(e);
        throw cause.getCause();
    }

Upvotes: 0

Views: 2481

Answers (2)

Chris Knight
Chris Knight

Reputation: 25064

Change your method signature to this:

public void someMethod() throws IOException
{   
    //some code
}

Have a look at this site for some useful information on checked exceptions and a little on the difference between checked and unchecked exceptions

Upvotes: 2

djechlin
djechlin

Reputation: 60748

Declare IOException as a checked exception in your function's signature.

Upvotes: 0

Related Questions