2

我正在运行以下内容:-

    private void ConvertFile()

    {
        PrepElec1();
        MakeElec2();
        MakeElec3();
        MakeElec4();
        MakeElecMerged();
        SetDataSet.SetData(DtSet);
        btnConvert.Enabled = false;
        btnReport.Visible = true;
    }

我怎样才能实现 try/catch,以便如果其中一个失败......

        PrepElec1();
        MakeElec2();
        MakeElec3();
        MakeElec4();
        MakeElecMerged();

...我收到一个消息框,显示它在哪一步失败?

4

1 回答 1

2

您可以让异常的堆栈跟踪为您完成工作。

private void ConvertFile()
{
    try
    {
        PrepElec1();
        MakeElec2();
        MakeElec3();
        MakeElec4();
        MakeElecMerged();
        SetDataSet.SetData(DtSet);
        btnConvert.Enabled = false;
        btnReport.Visible = true;
    }
    catch (Exception e)
    {
        var trace = e.StackTrace;

        // format trace however you like

        MessageBox.Show(this, trace, "Error");
    }
}
于 2013-01-20T14:57:06.290 回答