阶梯式错误处理

时间:2013-01-20 14:51:47

标签: c# try-catch

我正在运行以下内容: -

    private void ConvertFile()

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

如何实现try / catch,以便其中一个失败...

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

...我收到一个消息框,显示失败的步骤?

1 个答案:

答案 0 :(得分:2)

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

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");
    }
}