user956021
user956021

Reputation: 163

Throwing back Exception in catch block asks for another try catch block

I am trying to throw Exception Back to calling one. so i am catching the exception and throwing it . while throwing it ask me to surround the statement in try catch block.

catch(IOException e){
 throw new Exception(e);//ask me to surround with try catch
}

just want to know why is it saying like that.

Upvotes: 2

Views: 1958

Answers (6)

Mikhail
Mikhail

Reputation: 4223

This is because Exception is checked. You need to rethrow a subclass of RuntimeException. Usually IllegalArgumentException or IllegalStateException are used

public void myMethod(){
 try{
   // implementaion

 }catch(IOException e){
   LOG.error(e);
   throw new IllegalArgumentException("Bad file: ", e);
 }
}

Upvotes: 0

DrLivingston
DrLivingston

Reputation: 818

As the others point out, your method signature needs to state what exception types it will throw.

But more importantly, if you aren't creating a new specific type of exception there is no need to new another exception. Just rethrow the one you caught throw e;.

Or if you aren't going to do anything with it, don't even bother catching it, let it bubble up.

Upvotes: 0

Kallja
Kallja

Reputation: 5472

You need to declare the desired exeption type to be thrown by your method. Please do note that throwing a plain instance of the Exception class is a very bad idea.

public void myMethod() throws Exception {
  try {
    // Do something that may cause an IOException
  } catch(IOException e){
    throw new Exception(e);
  }
}

When you throw an instance of Exception (one that is not an instance of any subclass) you can't really do specialized exception handling. The throws Exception statement in your method signatures is likely to wind up cascading through your class hierarchy like a disease. Consider throwing a more specific (perhaps custom) Exception instance.

In some cases it may be feasible not to handle the exception thrown by the standard Java classes where it is originally thrown, but rather throw it back to the caller using the following code:

public void myMethod() throws IOException {
    // Do something that may cause an IOException
}

Upvotes: 1

Kick
Kick

Reputation: 4923

You are catching IOException and throwing Exception.You need to throws Exception from method.

Upvotes: 1

Ravinder Reddy
Ravinder Reddy

Reputation: 24012

Your method signature might have not defined to throw an Exception. Add throws Exception to your method and it should be working.

Or else

You must only be throwing a sub class of Exception but not parent Exception which is not acceptable. And hence you need to catch it or throw explicitly.

Upvotes: 0

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35587

Yes you are catching IOException while you want to throw an Exception. You have to add throws Exception to method.

public void myMethod() throws Exception{
 try{
   // implementaion

 }catch(IOException e){
   throw new Exception(e);
 }
}

Upvotes: 4

Related Questions