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
public static void Main()
//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());
Console.WriteLine("Result = {0}", Result);
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");
Console.WriteLine(ex.Message);
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
public static void Main()
Console.WriteLine("Please enter First Number");
//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);
Console.WriteLine("Please enter Second Number");
bool isValidSNO = int.TryParse(Console.ReadLine(), out SNO);
if (isValidSNO && SNO != 0)
Console.WriteLine("Result = {0}", Result);
//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");
Console.WriteLine("Only numbers between {0} && {1} are allowed",
Int32.MinValue, Int32.MaxValue);
Console.WriteLine("Only numbers between {0} && {1} are allowed",
Int32.MinValue, Int32.MaxValue);
Console.WriteLine(ex.Message);
OUTPUT:
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.
0 comments:
Post a Comment
Note: only a member of this blog may post a comment.