Nesting of try-catch block in C#

In this article we’ll discuss how to work with Nesting of try-catch block in C# program with the help of example. In C#, you can nest the try-catch block and also can use multiple catch blocks in C# to handle different types of exceptions.

Can try block be nested in C#?

Yes, nesting of the try-catch block is allowed in C#. The nesting of try-block means one try block can be nested into another try block. The various programmer uses the outer try block to handling major exceptions, whereas the inner try block for handling normal or minor exceptions. My personal opinion is that nesting of try-catch reduces the readability of the program so we should use is where it is highly required.

Important points about try-catch block in C#

  • It is necessary condition that a try block must be followed by a catch or finally blocks because if you use a finally block without a catch and if error occurred then it will be bubbled up to parent method catch block.
  • If any error occurred in inner, try block then immediately after the exception control is transferred to inner catch and all next code (in inner try block) is skipped from execution.    
  • If an exception raises in the inner try block that is not caught by the inner catch block, then that exception is promoted to the outer/parent try block. In this whole process nature/type of exception is changed.

Syntax:

Scenario: Let’s create 2 methods, parent and child method. Child is getting called by parent and both have try-catch block in them. If some error occurred in child method, then how it changes in parent method, let’s see below

If inner, try-catch in child method is creating some collection object and on error (may be network connection error) it will return null in collection object to its parent method and parent method try block will try to extract some data form it then object reference error may occur in outer block i.e., in parent function whereas in inner catch block error nature/ type was different

Nesting of try-catch block in C# in same method:

// START of outer try block
SqlConnection connection = new SqlConnection();
try
{
   //START of inner try-catch block
   try
     {
      connection.Open();
       
     }
   catch(IndexOutOfRangeException ex)
     {
       // code...
     }
  //END of inner try-catch block
}
catch(Exception ex)
  {
    // code...
  }
// END of outer catch block

In above example I did not add IF check while opening SQL connection. In case Connection already opened then it will give error. Inner catch block will not handle it because it is designed to handle different type (IndexOutOfRangeException) of exception and error will go to its parent catch block. This is the way how nested try-catch works.

Can we use try without catch C#?

Yes, but we need to use it with finally, like try-finally but if any error occurred in Try that will not get handled and will be moved to outer block catch or to parent method.

Do try-catch blocks or its nesting hurt performance?

Yes, try-catch will “hurt” performance. Although it is good practice to have try-catch in your code to prevent unhandled exception but massive use of this can defiantly slow down the program because compiler also need time to compile/ execute this extra line of codes.

In this article, we’ve learned how to do nesting of try-catch blocks in C#. You can also check our related article multiple catch blocks in C#. I believe after reading this article you’ll be able to use this in your daily development practices. You can also check more on Microsoft Learn.

Are you willing to check more on C# then please visit HERE

Leave a Reply

Your email address will not be published. Required fields are marked *