3

Would there be any value using a try catch statement inside another try block?

    try{
        // some code...
        try{
            // some code...
        }catch(Exception ex){
            ex.printStackTrace();
        }
        // some code...
    }catch(Exception ex){
        ex.printStackTrace();
    }
2
  • please add more information about your question.
    – KhAn SaAb
    May 29, 2013 at 8:46
  • 1
    In this case, your third "some code" will be executed even if your second "some code" throws an exception. If you remove the inner try-catch, it won't.
    – Burkhard
    May 29, 2013 at 8:47

8 Answers 8

3

Yes. You might want to catch a very specific exception in the inner block, which you could handle and return to the remainder of the block...

You would normally only do this with more specific Exceptions in the inner block though, rather than the catchall.

1

It could make sense if you catch a different exception in the nested catch. I would use the nested one to catch a more specific exception rather than a generic one.

1

Normally if you'll get different Exceptions, you just add catch statements to your try/catch, respecting the hierarchical order...

For example:

try{

}catch(IOException io){ //This catch is if you know that a IOException can occur

}catch(Exception e){ //This catch is if other exception not expected happens

}
1

Yes,

try
{
 int i=1;

 try
       {


       j=i/0;      //caught error
       }
catch(Exception e)
 {
 }

 j=i/1;

   ...               //continue execution
   ...

  }
  catch(Exceptione e)
{

}
1

Of course it makes sense. If the second 'some code' block throws an exception it can make sense to execute the third 'some code' block anyway. However, one has to make sure that the third block does NOT rely on any results of the second block.

1

one reason I can think of, you want to run a block of code where several exceptions may be thrown, of which some should break the flow of the application, and some shouldn't.

public void storePerson(Person input){
  try{
    validatePerson(); // if person is not valid, don't go through the flow
    try{
      writeInLog("I will be storing this person: " + input.getName());
    }
    catch(Exception e){
      System.out.println("Should have generated a logFile first, but hell, this won't put the flow in jeopardy.");
    }
    performPersistenceTasks(input);

  }
  catch(Exception e){
    e.printStackTrace();
    throw new Exception("Couldn't store the person");
  }
}
0

Sometimes a situation may arise where a part of a block may cause one error and the entire block itself may cause another error. Nested try is useful in these cases

0

Generally I would avoid it. It is using Exceptions too much to handle program control flow, and it looks damn ugly. IMO you need to have the Exception blocks as flat as possible. Only use this embedded try/catch in exceptional circumstances.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.