Exception Handling Abuse in C#

Exception Handling Abuse in C# with an Example

In this article, I am going to discuss the Exception Handling Abuse in C# and then we will see how to prevent Exception Handling Abuse in C# with one example. Please read our previous article where we discussed the Inner Exception in C# with an example.

Exceptions are nothing but runtime errors that occur during the execution of a program. For example, when an application is executing a database query, due to some reason the database connection is lost, then we will get SQLException runtime error. Exception handling is generally used to handle these scenarios. 

But sometimes, as a programmer, we are using exception handling mechanisms to implement programming logic which is bad, and this is called as exception handling abuse.

Example1: Here we are using exception handling to implement logical flow:
namespace ExceptionHandlingDemo
{
    class Program
    {
        public static void Main()
        {
            try
            {
                //Convert.ToInt32() can throw FormatException, if the entered value
                //cannot be converted to integer. So use int.TryParse() instead

                Console.WriteLine("Please enter First Number");
                int FNO = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine("Please enter Second Number");                
                int SNO = Convert.ToInt32(Console.ReadLine());

                int Result = FNO / SNO;
                Console.WriteLine("Result = {0}", Result);
            }
            catch (FormatException)
            {
                Console.WriteLine("Only numbers are allowed!");
            }
            catch (OverflowException)
            {
                Console.WriteLine("Only numbers between {0} & {1} are allowed",
                    Int32.MinValue, Int32.MaxValue);
            }
            catch (DivideByZeroException)
            {
                Console.WriteLine("Secoond Number cannot be zero");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }   
    }
}

Preventing exception handling abuse in C#:

Let’s rewrite the same example that doesn’t use exception handling to control the program’s logical flow.

namespace ExceptionHandlingDemo
{
    class Program
    {
        public static void Main()
        {
            try
            {
                Console.WriteLine("Please enter First Number");
                int FNO;
                //int.TryParse() will not throw an exception, instead returns false
                //if the entered value cannot be converted to integer
                bool isValidFNO = int.TryParse(Console.ReadLine(), out FNO);
                if (isValidFNO)
                {
                    Console.WriteLine("Please enter Second Number");
                    int SNO;
                    bool isValidSNO = int.TryParse(Console.ReadLine(), out SNO);
                    
                    if (isValidSNO && SNO != 0)
                    {
                        int Result = FNO / SNO;
                        Console.WriteLine("Result = {0}", Result);
                    }
                    else
                    {
                        //Check if the second number is zero and print a friendly error
                        //message instead of allowing DivideByZeroException exception 
                        //to be thrown and then printing error message to the user.
                        if (isValidSNO && SNO == 0)
                        {
                            Console.WriteLine("Second Number cannot be zero");
                        }
                        else
                        {
                            Console.WriteLine("Only numbers between {0} && {1} are allowed",
                                Int32.MinValue, Int32.MaxValue);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Only numbers between {0} && {1} are allowed",
                                Int32.MinValue, Int32.MaxValue);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }   
    }
}
OUTPUT:

Exception Handling Abuse in C#

In the next article, I am going to discuss the Delegates in C# with some real-time examples. Here, in this article, I try to explain the Exception Handling Abuse step by step with one example. I would like to have your feedback. Please post your feedback, question, or comments about this article.

1 thought on “Exception Handling Abuse in C#”

Leave a Reply

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