DRTauli
DRTauli

Reputation: 771

Try catch statement inside the try block

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();
    }

Upvotes: 3

Views: 2248

Answers (8)

Stultuske
Stultuske

Reputation: 9437

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");
  }
}

Upvotes: 1

Bernd Ebertz
Bernd Ebertz

Reputation: 1327

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.

Upvotes: 1

Senthil
Senthil

Reputation: 1244

Yes,

try
{
 int i=1;

 try
       {


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

 j=i/1;

   ...               //continue execution
   ...

  }
  catch(Exceptione e)
{

}

Upvotes: 1

Oliver Watkins
Oliver Watkins

Reputation: 13539

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.

Upvotes: 0

Manuel Pires
Manuel Pires

Reputation: 637

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

}

Upvotes: 1

rags
rags

Reputation: 2590

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

Upvotes: 0

Massimiliano Peluso
Massimiliano Peluso

Reputation: 26737

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.

Upvotes: 1

KingCronus
KingCronus

Reputation: 4529

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.

Upvotes: 3

Related Questions