4

I have just been thinking about this, as it is possible but I want to know if it is considered "bad practice". I believe it is, but I want to ask for views on it to check my opinion.

Is it bad to do something like this:

try{
    something();
    somethingelse();
        try{
            foo();
            bar();
        }catch(Exception e){
            e.printStackTrace();
        }
    somethingelseagain();
}catch(Exception e){
     e.printStackTrace();
}

I think there should never be a need to do this, since anything that throws an exception would trigger the first catch anyway.

Views are greatly appreciated.

dmck
  • 7,801
  • 7
  • 43
  • 79
ThePerson
  • 3,048
  • 8
  • 43
  • 69
  • 1
    It's catching Exception that's bad,not nesting try/catch. That said, nested try/catches almost always mean you should refactor. – Dave Newton Nov 22 '12 at 21:47
  • 1
    Unless of course `something`, `somethingelse` or `somethingelseagain` throw an exception, in which case they wouldn't be caught by the inner catch block. – Philip Kendall Nov 22 '12 at 21:49
  • nested try/catch blocks are usually better pulled into their own private method. However they are often not necessary if you are shrinking your try blocks as small as possible around the Exception which may be thrown. – Cory Kendall Nov 22 '12 at 21:56

3 Answers3

4

In your example, as written, the inner catch is a bit redundant.

However, nested catches would have more use in circumstances such as:

  • catching different exceptions

  • doing something different in the handler block for the two catches

  • inner catch might catch the exception, do something, then re-throw the same exception which the out catch block could catch

Also, don't forget the potential use of the finally block, which can execute cleanup code even if an exception was caught.

You should generally try to catch the most explicitly typed (i.e. specific) exception(s) as possible.

The subject of exceptions is an interesting one, and not without controversy.

Community
  • 1
  • 1
occulus
  • 16,959
  • 6
  • 53
  • 76
  • This is mostly a good answer I think, but I suppose my question to this is.. if you want to catch different exceptions, you would surely just have multiple catch statements with the one try. – ThePerson Nov 22 '12 at 21:53
  • 1
    Yup, if that would catch the behaviour you want, then it's certainly better than nesting. – occulus Nov 22 '12 at 21:55
  • Hi NutterzUK, would you consider accepting my answer if you found it useful? Cheers! – occulus Nov 30 '12 at 11:12
1

A case that I think you might want to nest try statements is if you request user input in your catch and that input might be invalid.

vtshadow
  • 125
  • 6
0

Nested try-catch is fully justified when circumstances call for it. For example, you don't want to skip any steps and break out of the main try if a certain method throws a certain exception. If it throws any other exception, then you want the normal behavior.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436