csharp Exception Questions & Answers

Question 6: What will be the output of the following program?

public static void Main()
{
    int[] arr = {1, 2, 3, 4, 5};
    try{
        Console.Write(arr[10]);
    }
    catch(IndexOutOfRangeException  ex){
        Console.WriteLine("Index error occurred!");
    }
    catch(NullReferenceException  ex){
        Console.WriteLine("Null error occurred!");
    }
    catch(Exception ex){
        Console.WriteLine("Error occurred!");
    }
}

Question 7: What will be the output of the following program?

public static void Main()
{
    int[] arr = {1, 2, 3, 4, 5};
	try{
        Console.Write(arr[10]);
    }
    catch(Exception ex){
		Console.WriteLine("Error occurred!");
	}
	catch(IndexOutOfRangeException  ex){
        Console.WriteLine("Index error occurred!");
    }
    catch(NullReferenceException  ex){
        Console.WriteLine("Null error occurred!");
    }
}

Question 8: What will be the output of the following program?

public static void Main()
{
    int[] arr = {1, 2, 3, 4, 5};
	try{
        Console.Write(arr[10]);
    }
    catch(Exception ex){
        Console.WriteLine("Error occurred!");
    }
    finally
    {
        Console.WriteLine("Executing finally block");
    }
	
	Console.WriteLine("Executing remaining program.");
}
ADVERTISEMENT

Question 9: Select the correct output of the following program?

public static void Main()
{
    int[] arr = {1, 2, 3, 4, 5};
    try{
        try{
            Console.Write(arr[10]);
        }
		catch(NullReferenceException ex){
			Console.WriteLine("Null error occurred!");
		}
        catch(Exception ex){
			Console.WriteLine("Error occurred!");
		}
	}
	catch(Exception ex){
		Console.WriteLine("Handling exception.");
	}
	Console.WriteLine("Execute remaining program.");
}

Question 10: What will be the output of the following program?

public static void Main()
{            
	int val = GetVal();
}
	
public static int GetVal(){
	int[] arr = {1, 2, 3, 4, 5};
    
    try{
        return arr[10];
    }
    catch(Exception ex){
        Console.WriteLine("Error occurred!");
    }
    finally{
	    return 0;
	}
}