Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 处理所有出口的通用方法_C#_.net_Exception - Fatal编程技术网

C# 处理所有出口的通用方法

C# 处理所有出口的通用方法,c#,.net,exception,C#,.net,Exception,我已经编写了一个函数,它可能会抛出各种异常 public class MyClass { private void Test(string param) { if (param.Length > 10) throw new ArgumentException(); else if (param.Length > 20) throw n

我已经编写了一个函数,它可能会抛出各种异常

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..
        }
    }
公共类MyClass
{
专用无效测试(字符串参数)
{
如果(参数长度>10)
抛出新ArgumentException();
否则,如果(参数长度>20)
抛出新的OverflowException();
else if(string.IsNullOrWhiteSpace(param))
抛出新ArgumentException();
否则如果(参数长度<1)
抛出新的FormatException();
}
公共无效调用(字符串输入)
{
尝试
{
测试(输入);
}
捕获(例外情况除外)
{
手部异常(ex);
}
}
私有无效句柄异常(异常除外)
{
//检查ex是否为ArgumentException类型
//待办事项。。
//检查ex是否为OverflowException类型
//待办事项。。。
//检查ex是否为ArgumentException类型
//待办事项。。
//检查ex是否为FormatException类型
//待办事项。。
}
}
是否可以使用HandleException(ex)私有方法来处理所有异常。否则,我必须为每个表达式编写单独的异常块

private void HandleException(Exception ex)
{
    if (ex is ArgumentException)
    {
    //ToDo..
    }
    else if (ex is OverflowException)
    {
    //ToDo..
    }
    else if (ex is FormatException)
    {
    //ToDo..
    }
}
如果“
is
vs
as
”的性能对您非常重要,您可以使用这种方法

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..
    }
}

为什么?与其在10行之后抛出一个要处理的异常,为什么不将ToDo向上移动10行,并在何时何地检测到问题?这是一个真实的例子吗?可能的重复事实上,我想在每个可能发生异常的地方使用HandleException(ex)。该类实际上如何处理其中的一些异常?如果
test
传递了一个长或短字符串,则调用者出错,可能应该通知调用者。它工作正常。但是,我是否必须在if/else块中执行向下转换?