Interview Questions: Multiple Catch Block in C#

Introduction

In C#, You can use more than one catch block with the try block to handle different types of exceptions means each catch block is used to handle different types of exceptions.

For example:

Below example, we have added multiple catch block, so let say if the user has entered string value in first input then control will be passed to the FormatException catch block.

using System;

namespace ExceptionExample
{
    class Program
    {
        static void Main()
        {
            try
            {
                Console.WriteLine("Enter a number:");
                var n = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine($"Enter a number to divide: {n}");
                var n1 = Convert.ToInt32(Console.ReadLine());

                var result = n / n1;
                Console.WriteLine($"Result: {result}");
            }
            catch (DivideByZeroException ex)
            {
                Console.Write(ex.Message);
            }
            catch (InvalidOperationException ex)
            {
                Console.Write(ex.Message);
            }
            catch (FormatException ex)
            {
                Console.Write(ex.Message);
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
            Console.ReadLine();
        }
    }
}

Here are some of the different case/questions which will help us to understand more about catch block, Interview question related to multiple catch blocks:

Interview Question: What is output of below program?

using System;

namespace ExceptionExample
{
    class Program
    {
        static void Main()
        {
            try
            {

                var result = 87 / 0;
                Console.WriteLine($"Result: {result}");
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
            catch (DivideByZeroException ex)
            {
                Console.Write(ex.Message);
            }            
            Console.ReadLine();
        }
    }
}

Answer:

Above code will give a compile-time error with message “A previous catch clause already catches all exceptions of this or of a super type (‘Exception’)”

As adding catch(exception) at starting of all catch blocks can suppress all other exception blocks hence all other exceptions are meaningless hence C# gives an error


Interview Question : What is output of below program if user provides string in input?

using System;

namespace ExceptionExample
{
    class Program
    {
        static void Main()
        {
            try
            {
                Console.WriteLine("Enter a number:");
                var n = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine($"Enter a number to divide: {n}");
                var n1 = Convert.ToInt32(Console.ReadLine());

                var result = n / n1;
                Console.WriteLine($"Result: {result}");
            }
            catch (DivideByZeroException ex)
            {
                Console.Write(ex.Message);
            }
            catch (InvalidOperationException ex)
            {
                Console.Write(ex.Message);
            }
            catch (FormatException ex)
            {
                Console.Write(ex.Message);
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
            Console.ReadLine();
        }
    }
}

Answer:

Only the respective error catch block gets executed on exception, so if the user enters string data in the input FormatException catch block will get execute after catch block end control directly pass to the Console.ReadLine(); statement.


Interview Question : What is output of below program if user provides string in input?

using System;

namespace ExceptionExample
{
    class Program
    {
        static void Main()
        {
            try
            {
                Console.WriteLine("Enter a number:");
                var n = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine($"Enter a number to divide: {n}");
                var n1 = Convert.ToInt32(Console.ReadLine());

                var result = n / n1;
                Console.WriteLine($"Result: {result}");
            }
            catch
            {
                Console.Write("Error occured!");
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
            Console.ReadLine();
        }
    }
}

Answer :

Above code will produce compile-time error with message Catch clauses cannot follow the general catch clause of a try statement

When we don’t provide any param to catch block it will be similar like catch (Exception ex) hence above code will produce an error.


Interview Question: What is the output of above programme?

using System;
using System.IO;

namespace ExceptionExample
{
    class Program
    {
        static void Main()
        {
            try
            {
                throw new FileNotFoundException();
            }
            catch (DivideByZeroException ex)
            {
                Console.Write(ex.Message);
            }
            catch (InvalidOperationException ex)
            {
                Console.Write(ex.Message);
            }
            catch (FormatException ex)
            {
                Console.Write(ex.Message);
            }

            Console.ReadLine();
        }
    }
}

Answer :

Above program will abruptly terminates the program, as above programme will throwing FileNotFoundException and which is not handled.


Interview Question : What is the output of above programme?

using System;
using System.IO;

namespace ExceptionExample
{
    class Program
    {
        static void Main()
        {
            try
            {
                throw new FileNotFoundException();
            }
            catch (DivideByZeroException ex)
            {
                Console.Write(ex.Message);
            }
            catch (InvalidOperationException ex)
            {
                Console.Write(ex.Message);
            }
            catch (FormatException ex)
            {
                Console.Write(ex.Message);
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
            Console.ReadLine();
        }
    }
}

Answer:

When the specific type of error catch block is not available then it defaults catch will executes.


Interview Question : How to catch all exceptions using a single catch block?

Yes, we can catch all exceptions with a single catch block with parameter “Exception”. We should use this catch block only for stopping abnormal termination irrespective of the exceptions thrown from its corresponding try block.

It is always recommended to write catch blocks with exception parameters even though we are writing multiple catch blocks. It acts as a backup catch block.


You can read more about exception handling in C#

Leave a Reply

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