Skip to content

Instantly share code, notes, and snippets.

@yeasin90
Created November 3, 2014 20:23
Show Gist options
  • Save yeasin90/88992e5375e5eff062dc to your computer and use it in GitHub Desktop.
Save yeasin90/88992e5375e5eff062dc to your computer and use it in GitHub Desktop.
Common method for handling all C# exceptions
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