Created
November 3, 2014 20:23
-
-
Save yeasin90/88992e5375e5eff062dc to your computer and use it in GitHub Desktop.
Common method for handling all C# exceptions
This file contains 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
public class MyClass | |
{ | |
private void Test(string param) | |
{ | |
if (string.IsNullOrWhiteSpace(param)) | |
throw new ArgumentNullException("Null Ex"); | |
else if (param.Length > 1) | |
throw new ArgumentException("Arg Excp"); | |
else if (param.Length > 5) | |
throw new OverflowException(); | |
else if (param.Length < 1) | |
throw new FormatException(); | |
} | |
public void Call(string input) | |
{ | |
try | |
{ | |
Test(input); | |
} | |
catch (Exception ex) | |
{ | |
HandleException(ex); | |
} | |
} | |
private void HandleException(Exception ex) | |
{ | |
ArgumentException argEx; | |
OverflowException ovfEx; | |
FormatException fmtEx; | |
ArgumentNullException argNullExp; | |
if ((argNullExp = ex as ArgumentNullException) != null) | |
{ | |
//ToDo.. | |
} | |
else if ((argEx = ex as ArgumentException) != null) | |
{ | |
//ToDo.. | |
} | |
else if ((ovfEx = ex as OverflowException) != null) | |
{ | |
//ToDo.. | |
} | |
else if ((fmtEx = ex as FormatException) != null) | |
{ | |
//ToDo.. | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment