2013-01-20 38 views
2

我運行了以下內容: -階梯錯誤處理

private void ConvertFile() 

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

我怎樣才能impliment try/catch語句,這樣,如果其中一個出現故障......

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

...我得到一個消息框顯示它在哪一步失敗?

回答

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

完美。謝謝! – user1936588