What will be the output of the following C# code snippet? [code] class Program { public static void Main(string[] args) {

Stasenndb

New member
What will be the output of the following C# code snippet?
Code:
 class Program
 {
     public static void Main(string[] args)
     {
         try
         {
             int a = 1;
             int b = 10 / a;
             try
             {
                 if (a == 1)
                     a = a / a - a;
                 if (a == 2)
                 {
                     int[] c = { 1 };
                     c[8] = 9;
                 }
             }
             finally
             {
                 Console.WriteLine("A");
             }
        }
        catch (IndexOutOfRangeException e)
        {
             Console.WriteLine("B");
        }
        Console.ReadLine();
    }
 }
A. A
B. B
C. AB
D. BA
 
Answer: a
Explanation: The inner try block does not have a catch which can tackle IndexOutOfRangeException hence finally is executed which prints ‘A’. The outer try block does have catch for IndexOutOfBoundException exception but no such exception occurs in it hence its catch is never executed and only ‘A’ is printed.
 

Tìm kiếm

Back
Top