WinForms异常处理

时间:2016-05-06 20:42:59

标签: c# error-handling exception-handling

我有一个从db填充数据集的方法,看起来或多或少像这样:

private DataSet GetData(string query)
{
    try
    {
        //do some stuff to populate dataset
        return dataset;
    }
    catch (SqlException ex)
    {
        MessageBox.Show("There was a database error. Please contact administrator.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        LogExceptionToFile(ex); //to log whole exception stack trace, etc.
    }
    finally
    {
        //cleanup
    }
}

//calling methods:
GetData(query);
OtherMethod1(); //this method shows message box of success

如果我在异常的情况下拥有那段代码,我会收到用户友好的消息框,然后调用OtherMethod1()并显示成功消息框。如果在GetData()中出现错误,我想停止。当我在消息框之后向此catch块添加throw;时,会显示另一个消息框,而不是抛出未处理的异常。如果我提供了友好的信息,我想避免显示第二个消息框。

5 个答案:

答案 0 :(得分:2)

您可以返回表示成功的值:

<asp:ListView ID="ListView1" runat="server" 
    DataSourceID="Details" ItemType="MyNamespace.MyClass">

<asp:TextBox ID="editpassword" runat="server" 
    Value='<%# BindItem.Password %>' Text="New Password">
</asp:TextBox>

答案 1 :(得分:1)

如果我正确理解您的困境,您可以重新抛出并处理(当前未处理的)异常,如下所示:

private DataSet GetData(string query)
{
    try
    {
        //do some stuff to populate dataset
        return dataset;
    }
    catch (SqlException ex)
    {
        MessageBox.Show("There was a database error. Please contact administrator.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        LogExceptionToFile(ex); //to log whole exception stack trace, etc.

        throw;
    }
    finally
    {
        //cleanup
    }
}


//calling methods:
try
{
    GetData(query);
    OtherMethod1(); //this method shows message box of success
}
catch(Exception ex)
{
    //Do whatever you need to do here, if anything.
}

现在,这肯定不是唯一的方法,我只是告诉你如何做你听到的尝试。其中一些答案也很棒,可能更适合您的特定情况。

答案 2 :(得分:0)

我会说你可以使用回调。

Here's a link on returning multiple values

使用回调获取GetData()方法返回多个值。然后,您可以根据success方法提供的callBack值来设置GetData() bool。

然后在您的调用代码中,如果OtherMethod()布尔为success,则只运行true

您甚至可以将ex异常作为回调的一部分返回,并使用一段代码来显示对话框;无论是成功还是失败,如果是这样,都会在其中显示例外情况。

示例:

private DataSet GetData(string query, Action<bool> callBack)
{
    bool successful = false; // This will be returned in the callback.
    DataSet returnValue; // This will be the dataset stuff.
    try
    {
        //do some stuff to populate dataset
        returnValue = ???; // Populate your return value , but don't return yet;
        successful = true; // This will indicate success.
    }
    catch (SqlException ex)
    {
        MessageBox.Show("There was a database error. Please contact administrator.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        LogExceptionToFile(ex); //to log whole exception stack trace, etc.
    }
    finally
    {
        //cleanup

        if (callBack != null) // Send the callback with the boolean of successful.
        {
            callBack(successful);
        }

        return returnValue; // Now return your DataSet.
    }
}

bool success = false; // Use this to determine if GetData was successful.
//calling methods:
GetData(query, (s) => success = s);

if (success)
{
    OtherMethod1(); //this method shows message box of success
}
else
{
    // Do something else, or show your failure message box.
}

那种方式更清洁。

希望这有帮助!

答案 3 :(得分:0)

如果出现错误,您只需返回null即可。 (尽管可能不鼓励使用null取决于您与谁交谈或您的公司或项目中的编码指南可能是什么。)

private DataSet GetData(string query)
{
    try
    {
        //do some stuff to populate dataset
        return dataset;
    }
    catch (SqlException ex)
    {
        MessageBox.Show("There was a database error. Please contact administrator.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        LogExceptionToFile(ex); //to log whole exception stack trace, etc.
    }
    finally
    {
        //cleanup
    }

    return null;
}

//calling methods:
var result = GetData(query);
if (result != null) 
    OtherMethod1(); //this method shows message box of success

答案 4 :(得分:0)

您可以在throw;中将catch添加到GetData()

private DataSet GetData(string query)
{
    try
    {
        //do some stuff to populate dataset
        return dataset;
    }
    catch (SqlException ex)
    {
        MessageBox.Show("There was a database error. Please contact administrator.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        LogExceptionToFile(ex); //to log whole exception stack trace, etc.
        throw;
    }
    finally
    {
        //cleanup
    }
}

然后执行此操作:

try
{
  GetData(query);
  OtherMethod1();
}
catch (Exception ex)
{
  // do something to ex if needed
}

这样您就不会获得第二个消息框,并且可以在需要时再次处理异常。