O'Reilly logo

C# Primer Plus by Klaus Michelsen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required