In Exception handling maximum how many times we apply Nested try-catch in C# Unable to find answer?
-
1Is this an academic question? Just curious what for you need this information. I think the compiler won't complain about 10 nested `try/catch` blocks, but surely everyone reading that code will. – René Vogt Mar 23 '16 at 10:37
-
http://stackoverflow.com/questions/13239972/how-do-you-implement-a-re-try-catch may be you are looking out for this – Uthistran Selvaraj. Mar 23 '16 at 10:39
-
This not an academic question? but we should know that. Someone asked that question to me. – Rohit Mane Mar 23 '16 at 10:44
-
I would say: as many times as the c# file still fits in memory, as many times are you are able to write (and execute and test) in a lifetime, as many times as you are able to find a useful example for it. – Stefan Steinegger Mar 23 '16 at 11:11
-
1Questions like these have a standard answer: if you need to know then you are doing it wrong. – Hans Passant Mar 23 '16 at 11:19
-
Who say's.. its not wrong question. Right now I'm using nesting at 5 levels.. – Rohit Mane Mar 23 '16 at 11:24
-
5 levels is wrong. Exception handling is not a mechanism that should be used to control the application flow. If you do that you have fundamentally misunderstood what exceptions are. – jgauffin Mar 23 '16 at 11:28
-
@RohitMane That code doesn't say anything about why you *need* this. You could (and probably should) implement it without nesting. – Thorsten Dittmar Mar 23 '16 at 11:53
-
Really I've implemented code for some reasons... Used try-catch in this way.. try { } catch () { try { } catch () { try {} catch () {... } } – Rohit Mane Mar 23 '16 at 11:55
-
@RohitMane Yes, the way you do it you use exceptions to control program flow. That is not how they should be used. I have myself nested `try`-blocks in the **`finally`** block to catch exceptions during cleanup. – Thorsten Dittmar Mar 23 '16 at 11:57
-
**Why** should this be **opinion based**???? This question is never ever opinion based! – Stefan Steinegger Mar 24 '16 at 06:41
2 Answers
In my understanding (no proofs, only thoughts) it's not limited. I haven't said unlimited, but what there is no MaxNesting
property or constant somewhere.
try/catch/finally
is a C#
construction compiled into IL. The question is basically equal to
how deep can be [insert keyword] nesting
E.g.: switch
, if
, foreach
, etc.
Have you heard ever about maximum nesting of those? I don't think so.
There can be just one try/catch
in the recursive method calling itself inside try
. Does try
brings any limitations by itself? Yes, as this obviously have to use more memory to hold address of catch
somewhere. Can you say the number? Nope, it depends on available memory.

- 20,892
- 15
- 90
- 319
There can not be a limit if you read the documentation carefully, which says that a try
block surrounds guarded code that potentially throws an exception. Same goes for the catch
block, which surrounds the code to execute when an exception occurs and the finally
block.
As a try-catch-finally
block is itself by definition a code block, this definition is recursive, so any try
block can surround a try-catch-finally
block, any catch
and finally
block can, too.
The only limits are readability and memory.
but we should know that.
Why? What advantage do you gain or expect from that information? I'm very much with what @HansPassant said in the comments...
By the way: Nothing keeps you from writing a small test application that generates a C# file with a given maximum number of nesting levels, which you then try to compile. If you find a maximum, look at the source file and ask yourself if this is still readable.

- 55,956
- 8
- 91
- 139
-
Can you help me how to handle above situation in Exception Handling. May be the way I've chosen is wrong.. – Rohit Mane Mar 23 '16 at 12:21
-
Well, the code you provided in the comments doesn't say much about what you actually *do*. It just shows you have other `try-catch` blocks in the `catch` block. Maybe if you extended your question to show an example of what you do you'd get suggestions on how to improve it. – Thorsten Dittmar Mar 23 '16 at 12:29
-
@RohitMane Was this asked in an interview? I have had dumb interviewers ask me this and piss me off straightaway. Unless there was a limit of say 5-6 catch blocks, it will not come into picture in real life ever. But still dumb interviewers ask questions like this a lot. – jitendragarg Jun 02 '17 at 09:55