Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are nested Try/Catch blocks a bad idea?

Let's say we have a structure like so:

Try   ' Outer try code, that can fail with more generic conditions,    ' that I know less about and might not be able to handle    Try     ' Inner try code, that can fail with more specific conditions,     ' that I probably know more about, and are likely to handle appropriately   Catch innerEx as Exception     ' Handle the inner exception   End Try  Catch outerEx as Exception   ' Handle outer exception End Try 

I have seen some opinions that nesting Try blocks like this is discouraged, but I could not find any specific reasons.

Is this bad code? If so, why?

like image 988
Goro Avatar asked Jan 25 '11 22:01

Goro


People also ask

Is it bad to have nested try catch?

Nesting try-catch blocks severely impacts the readability of source code because it makes it to difficult to understand which block will catch which exception.

Can try catch blocks be nested?

Yes, we can declare a try-catch block within another try-catch block, this is called nested try-catch block.

How do you resolve a nested try catch?

How to Avoid the Nesting? Extracting the nested part as a new method will always work for any arbitrarily nested Try-Catch-Finally block. So this is one trick that you can always use to improve the code.

Why we use nested try catch?

As the name suggests, a try block within a try block is called nested try block in Java. This is needed when different blocks like outer and inner may cause different errors. To handle them, we need nested try blocks.


2 Answers

There are certain circumstances where they're a good idea, e.g. one try/catch for the whole method and another inside a loop as you want to handle the exception and continue processing the rest of a collection.

Really the only reason to do it is if you want to skip the bit that errored and carry on, instead of unwinding the stack and losing context. Opening multiple files in an editor is one example.

That said, exceptions should (as the name implies) be exceptional. A program should handle them but try to avoid them as part of normal execution flow. They're computationally expensive in most languages (Python being one notable exception).

One other technique which can be useful is catching specific exception types...

Try     'Some code to read from a file  Catch ex as IOException     'Handle file access issues (possibly silently depending on usage) Catch ex as Exception     ' Handle all other exceptions.     ' If you've got a handler further up, just omit this Catch and let the      ' exception propagate     Throw End Try 

We also use nested try/catches in our error handling routines...

    Try         Dim Message = String.Format("...", )         Try             'Log to database         Catch ex As Exception             'Do nothing         End Try          Try             'Log to file         Catch ex As Exception             'Do nothing         End Try     Catch ex As Exception         'Give up and go home     End Try 
like image 108
Basic Avatar answered Sep 19 '22 13:09

Basic


I actually don't think there's anything inherently wrong about nested Try/Catch blocks, except that they can be difficult to navigate and are likely a sign that you could do some refactoring (the inner Try/Catch into its own method, for example).

But I do want to address this comment:

' Outer try code, that can fail with more generic conditions,  ' that I know less about and might not be able to handle 

If you don't know how to handle exceptions in a particular situation, trust me: don't catch them. Better to let your app crash (I mean, you know, log it; just don't swallow it) than to catch something you don't know how to recover from and then let your app continue merrily on its way in a corrupted state. Behavior will be unpredictable at best from that point on.

like image 42
Dan Tao Avatar answered Sep 20 '22 13:09

Dan Tao