Conversation


Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!

Join the discussion and share your insights now!

Comments 0


advertisement

Nested try-catch Blocks in C# 

nested trycatch block in c#


Exception-handling code can be nested in a program. In nested exception handling, a try block can enclose another ty-catch block. In addition, a single try block can have multiple catch blocks that are sequenced to catch and handle different types of exceptions raised in the try block.

Nested try Blocks:

The nested try block consists of multiple try-catch constructs. A nested try block starts with a try block, which is called the outer try block. This outer try block contains multiple try blocks within it, which are called inner try blocks. if an exception is thrown by a nested try block, the control passes to its corresponding nested catch block.

Consider an outer try block containing a nested try-catch-finally construct. it the inner try block throws an exception, control is passed to the inner catch block. However, if the inner catch block does not contain the appropriate error handler, the control is passed to the outer catch block.


EXAMPLE


class program
{
      static void Main(string[] args)
     {
          string[] names = { "john", "james" };
          int numone = 133;
          int numtwo = 2;
          double result = 0;
     try
    {
          Console.WriteLine("This is Main try block");
          names[0] = "patrick";
          for(int i=0; i<2; i++)
          {
               Console.WriteLine("Names of students: "+names[i]);
          }
    try
   {
          Console.WriteLine("This is nested try block");
          result = numone / numtwo;
   }
   catch(ArithmeticException excobj)
   {
          Console.WriteLine("Divide by 0: "+excobj);
   }
   finally
   {
          Console.WriteLine("Result is: "+result);
    }
}
     catch(IndexOutOfRangeException excobj)
     {
          Console.WriteLine("Wrong number of arguments: "+excobj);
      }
          Console.ReadKey();
      }
}


Define

The provided C# code defines a console application within the "program" class. It initializes an array of strings named "names" with two elements. Two integer variables, "numone" and "numtwo," are also initialized with values 133 and 2, respectively, while a double variable "result" is set to 0. The outer try block begins with a message to the console and modifies the first element of the "names" array. It then enters a loop to print the names of students from the array. Inside this block, there is a nested try-catch-finally structure.

The nested try block attempts a division operation, catching any potential ArithmeticException, and prints an appropriate error message. The nested finally block ensures the display of the result of the division operation. The outer catch block handles any IndexOutOfRangeException that might occur, printing an error message with details. The program concludes with a command to wait for a key press before closing the console window. This code demonstrates the use of exception handling to address potential errors in array indexing and arithmetic operations.


C# ty-catch-finally exception handling nested try block nested try catch c# best practices multiple try block in c# exception handling in c# nested try block in c# nested try catch block in c# with example c# try-catch all exception nested try catch c#

advertisement