Nested try Blocks
You can insert a try block inside another try block to form nested try blocks. An unlimited number of nested try blocks can be written inside each other. Any uncaught exceptions of an inner try block moves through each of its outer try blocks one by one from innermost to outermost in search of a matching catch block. If no match is found, the runtime's default exception handler will handle the exception as shown earlier.
Listing 19.5 provides a simple example of one try-catch block nested inside another try block.
Listing 19.5. NestingTryBlocks.cs
01: using System; 02: 03: class TryNestTester 04: { 05: public static void Main() 06: { 07: try 08: { 09: try 10: { 11: int[] myArray = new int[10]; 12: 13: myArray[50] = 498; 14: ... |
Get C# Primer Plus now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.