0

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();
    }
Ali
  • 2,012
  • 3
  • 25
  • 41
  • 2
    You need to understand how checked exceptions work. – SLaks Mar 12 '13 at 22:17
  • I think your code in `catch` does nothing ... `throw e` would do the same ... – kkonrad Mar 12 '13 at 22:19
  • 3
    @kkonrad: Getting rid of it entirely would also do the same. He needs to understand how checked exceptions work. – SLaks Mar 12 '13 at 22:20
  • clearly. I though that maybe he would like to print that something happened and than re-throw this. Still - lack of knowledge about exceptions in java ... – kkonrad Mar 12 '13 at 22:21
  • It's not at all clear what the OP is trying to do, but it does look like omitting the `try`/`catch` entirely would have the same effect. – Louis Wasserman Mar 12 '13 at 22:22
  • 1
    He says it clearly: "I trying to throw inner exception in another exception ..." so I don't think skipping `try/catch` is solution for him ... – kkonrad Mar 12 '13 at 22:24

2 Answers2

2

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

Chris Knight
  • 24,333
  • 24
  • 88
  • 134
0

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

djechlin
  • 59,258
  • 35
  • 162
  • 290