Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.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_Exception Handling_Custom Exceptions - Fatal编程技术网

C# 如何为自定义控件或扩展方法创建自定义异常处理程序

C# 如何为自定义控件或扩展方法创建自定义异常处理程序,c#,.net,exception,exception-handling,custom-exceptions,C#,.net,Exception,Exception Handling,Custom Exceptions,我正在C#中创建一个扩展方法,从datagridview中检索一些值。 这里,如果用户给出的列名不存在,那么我希望此函数抛出一个异常,可以在调用此函数的位置处理该异常。 我怎样才能做到这一点 public static T Value<T>(this DataGridView dgv, int RowNo, string ColName) { if (!dgv.Columns.Contains(ColName))

我正在C#中创建一个扩展方法,从datagridview中检索一些值。 这里,如果用户给出的列名不存在,那么我希望此函数抛出一个异常,可以在调用此函数的位置处理该异常。 我怎样才能做到这一点

   public static T Value<T>(this DataGridView dgv, int RowNo, string ColName)
    {
            if (!dgv.Columns.Contains(ColName))
                throw new ArgumentException("Column Name " + ColName + " doesnot exists in DataGridView.");
            return (T)Convert.ChangeType(dgv.Rows[RowNo].Cells[ColName].Value, typeof(T));
    }
公共静态T值(此DataGridView dgv,int RowNo,string ColName)
{
如果(!dgv.Columns.Contains(ColName))
抛出新ArgumentException(“列名称”+ColName+“在DataGridView中不存在”);
返回(T)Convert.ChangeType(dgv.Rows[RowNo].Cells[ColName].Value,typeof(T));
}

很难理解您的问题,但听起来您想抛出一个异常并在调用扩展方法的地方处理它。如果是这样,你就快到了。您已经在抛出异常,只需在调用站点周围放置一个
try/catch

public static T Value<T>(this DataGridView dgv, int RowNo, string ColName)
{
    if (!dgv.Columns.Contains(ColName))
        throw new ArgumentException("Column Name " + ColName + " doesnot exists in DataGridView.");
    return (T)Convert.ChangeType(dgv.Rows[RowNo].Cells[ColName].Value, typeof(T));
}

// Wherever you call the method:
try
{
    dataGridView.Value(rowNumber, columnName);
}
catch (ArgumentException)
{
    // caught the exception
}
公共静态T值(此DataGridView dgv,int RowNo,string ColName)
{
如果(!dgv.Columns.Contains(ColName))
抛出新ArgumentException(“列名称”+ColName+“在DataGridView中不存在”);
返回(T)Convert.ChangeType(dgv.Rows[RowNo].Cells[ColName].Value,typeof(T));
}
//无论您在何处调用该方法:
尝试
{
值(行号、列名);
}
捕获(异常)
{
//抓住了例外
}

这就是你想要的吗?

这不是正确的方法。首先,用户输入错误并没有什么例外。第二,这种扩展方法很容易被调用,并深深嵌套在某种与数据库一起工作的代码中。您必须捕获异常,因为键入错误是正常的。但是现在您还需要编写异常处理程序和一组正确恢复程序状态的代码

验证用户输入应该在很难停止的代码序列出现之前尽早进行。在用户输入验证中没有理由使用异常,一个简单的if()语句就可以完成任务


现在可以保留throw语句,如果愿意,它确实有助于更好的诊断。但您永远不应该处理该异常,因为现在它会诊断代码中的错误。而且你不能用catch子句修复bug。

尽管这个问题已经得到了回答,我还是建议第二种解决方案。 抛出异常的想法应该是最后的选择。您可以使用以下方法实现相同的目标

public static bool TryGetValue<T>(this DataGridView dgv, int RowNo, 
    string ColName, out T cellValue)
{
    cellValue = default(T);
    if (!dgv.Columns.Contains(ColName))
        return false;
    cellValue = (T)Convert.ChangeType(dgv.Rows[RowNo].Cells[ColName].Value, typeof(T));
    return true;
}

public static void Main(){
int desiredValue;
if(dataGridView.TryGetValue<int>(rowNumber, columnName, out desiredValue)){
    //Use the value
}
else{
    //Value can not be retrieved.
}
}
public static bool TryGetValue(此DataGridView dgv,int RowNo,
字符串ColName,out cellValue)
{
cellValue=默认值(T);
如果(!dgv.Columns.Contains(ColName))
返回false;
cellValue=(T)Convert.ChangeType(dgv.Rows[RowNo].Cells[ColName].Value,typeof(T));
返回true;
}
公共静态void Main(){
整数期望值;
if(dataGridView.TryGetValue(行号、列名、out desiredValue)){
//使用值
}
否则{
//无法检索值。
}
}

PS:我还没有在编辑器上输入这段代码,所以请原谅任何打字错误

@Zach:你明白我的意思。但我有点困惑。假设我为此创建了一个程序集,然后将其包含到其他项目中。我不在他们的服务器上使用任何异常处理程序,那么异常会发生在哪里?我的意思是在我的dll或用户代码中。我感觉我的dll在这一点上会挂断。使用dll的用户将捕获他们的应用程序,其所有应用程序将异常关闭。是吗?@Shantanu:只要您将自定义异常类
公开
,您的其他项目就能够捕获您抛出的自定义异常。否则,另一个项目将崩溃(除非它捕获所有
异常,但这样做被认为是不好的做法)。