Created
March 17, 2015 06:24
-
-
Save dnasca/89b1e91b9d614b1a10a0 to your computer and use it in GitHub Desktop.
How to not abuse exception handling!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| /* Exception Handling abuse | |
| * | |
| * Using exception handling to implement program logical flow is _bad_ and is termed as exception handling abuse | |
| * | |
| * Below are 2 examples, the first, is an example of Exception Handling Abuse | |
| * the second, is an example of properly handling exceptions | |
| * | |
| */ | |
| #region Exception Handling Abuse | |
| //the code below is an example of exception handling abuse. | |
| //using exception handling to implement program logical flow is _bad_ | |
| class ExceptionHandlingAbuse | |
| { | |
| public static void TheWrongWay() | |
| { | |
| try | |
| { | |
| Console.WriteLine("Let's divide 2 integers..."); | |
| Console.Write("Enter the numerator: "); | |
| int numerator = Convert.ToInt32(Console.ReadLine()); //will only throw an exception if the conversion fails | |
| Console.Write("Enter the denominator: "); | |
| int denominator = Convert.ToInt32(Console.ReadLine()); | |
| int result = numerator/denominator; | |
| Console.WriteLine("Result = {0}", result); | |
| } | |
| catch (FormatException) | |
| { | |
| Console.WriteLine("Enter valid integers only."); | |
| } | |
| catch (OverflowException) | |
| { | |
| Console.WriteLine("That number is too large, only numbers between {0} and {1} are accepted.", Int32.MinValue, | |
| Int32.MaxValue); | |
| } | |
| catch (DivideByZeroException) | |
| { | |
| Console.WriteLine("You just tried to divide by 0... Wow."); | |
| } | |
| catch (Exception ex) | |
| { | |
| Console.WriteLine(ex.Message); | |
| } | |
| Console.ReadKey(); | |
| } | |
| } | |
| #endregion | |
| #region ExceptionHandlingAbuse_solved | |
| class ExceptionHandlingAbuseSolved | |
| { | |
| public static void Main() | |
| { | |
| try | |
| { | |
| Console.WriteLine("Let's divide 2 integers..."); | |
| Console.Write("Enter the numerator: "); | |
| int numerator; | |
| bool isNumeratorConversionSuccessful = Int32.TryParse(Console.ReadLine(), out numerator); | |
| //if the conversion is successful, store(out) the converted int value in 'numerator' and also store bool result as 'isNumeratorConversionSuccessful' | |
| if (isNumeratorConversionSuccessful) | |
| //if the numerator was successfully converted and stored as true, then move on | |
| { | |
| Console.Write("Enter the denominator: "); | |
| int denominator; | |
| bool isDenominatorConversionSuccessful = Int32.TryParse(Console.ReadLine(), out denominator); | |
| if (isDenominatorConversionSuccessful && denominator != 0) | |
| //if the conversion was successful AND the denominator is not 0, move on | |
| { | |
| int result = numerator / denominator; | |
| Console.WriteLine("Result = {0}", result); | |
| } | |
| else | |
| { | |
| if (denominator == 0) | |
| { | |
| Console.WriteLine("You just tried to divide by 0."); | |
| } | |
| else | |
| { | |
| Console.WriteLine("Denomenator should be a valid number between {0} and {1}", Int32.MinValue, | |
| Int32.MaxValue); | |
| } | |
| } | |
| } | |
| else | |
| { | |
| Console.WriteLine("Numerator should be a valid number between {0} and {1}", Int32.MinValue, Int32.MaxValue); | |
| } | |
| } | |
| catch (Exception ex) | |
| { | |
| Console.WriteLine(ex.Message); | |
| } | |
| Console.ReadKey(); | |
| } | |
| } | |
| #endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment