0

I have written a function that may throw various exceptions..

public class MyClass
    {
        private void Test(string param)
        {
            if (param.Length > 10)
                throw new ArgumentException();
            else if (param.Length > 20)
                throw new OverflowException();
            else if (string.IsNullOrWhiteSpace(param))
                throw new ArgumentException();
            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)
        {
            //Check if ex is of type ArgumentException
            //ToDo..

            //Check if ex is of type OverflowException
            //ToDo...

            //Check if ex is of type ArgumentException
            //ToDo.. 

            //Check if ex if of type FormatException
            //ToDo..
        }
    }

Is it possible to have the HandleException(ex) private method so that i can handle all exceptions. Otherwise, I have to write separate exception blocks for each expcetions

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
Yeasin Abedin
  • 2,081
  • 4
  • 23
  • 41
  • *why??* Rather than throw an exception to be handled 10 lines later, why not move the ToDo up 10 lines and handle the problem when and where it is detected? – Ňɏssa Pøngjǣrdenlarp Nov 03 '14 at 13:43
  • Is that a real example? – ken2k Nov 03 '14 at 13:43
  • possible duplicate of [How to catch ALL exceptions/crashes in a .NET app](http://stackoverflow.com/questions/82483/how-to-catch-all-exceptions-crashes-in-a-net-app) – Stormenet Nov 03 '14 at 13:44
  • actually i want to use the HandleException(ex) in every place where an exception may occur. – Yeasin Abedin Nov 03 '14 at 13:47
  • how can that class actually do anything about some of them? If `test` is passed a long or short string, the caller is in error and perhaps should be informed. – Ňɏssa Pøngjǣrdenlarp Nov 03 '14 at 13:47
  • Body of test function is dummy. test function may contain other logic from which different exceptions may occur. It may contain sql server operation or server side validation.. – Yeasin Abedin Nov 03 '14 at 13:50
  • I don't think that this approach is a good design. In certain contexts an Overflow Exception could be a dead end while in other contexts you could simply log it and continue with a default value. Using a generic do-it-all handler for every kind of exception doesn't seem to be a good practice unless you expect to have the same behavior for every kind of exception. – Steve Nov 03 '14 at 13:53
  • As i am manually throwing exception based on logics, all behavior are same for all exceptions, that is write into a log file. – Yeasin Abedin Nov 03 '14 at 14:01

1 Answers1

1
private void HandleException(Exception ex)
{
    if (ex is ArgumentException)
    {
    //ToDo..
    }
    else if (ex is OverflowException)
    {
    //ToDo..
    }
    else if (ex is FormatException)
    {
    //ToDo..
    }
}

if the performance of "is vs as" is so important for you you can use this approach

private void HandleException(Exception ex)
{
    ArgumentException argEx;
    OverflowException ovfEx;
    FormatException fmtEx;
    if ((argEx = ex as ArgumentException) != null)
    {
        //ToDo..
    }
    else if ((ovfEx = ex as OverflowException) != null)
    {
        //ToDo..
    }
    else if ((fmtEx = ex as FormatException) != null)
    {
        //ToDo..
    }
}
Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74