2012-05-01 62 views
0

我有一個代碼來重新啓動一個服務,該事件也執行其他功能。由於異常導致的錯誤消息處理

我在事件中嘗試捕捉像這樣的事件中的一切:

private void btnApply_Click(object sender, EventArgs e) 
    { 
     try 
     {    
      applyChangesAndCheckRestartService(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Error loading page.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
    } 

private void applyChangesAndCheckRestartService() 
    { 
     string svrPortNo = CommonCodeClass.getTagValue(CommonCodeClass.xml_SvrPortNoTag, CommonCodeClass.configLocation + CommonCodeClass.configXML);    

     if (!ApplyChangesForSettings()) 
     {     
      return; 
     } 

     if (svrPortNo != tbSvrPortNo.Text) 
     {    
      CommonCodeClass.CheckToRestartService(); 
     } 
    } 

現在如果有ApplyChangesForSettings過程中發生錯誤()我會得到一個錯誤彈出「錯誤加載頁面」。

如果在CheckToRestartService()中有錯誤,由於try catch我會得到相同的錯誤。

有沒有更好的方法來處理這個問題。

像我不介意ApplyChangesForSettings(),但爲CheckToRestartService()的錯誤加載頁面,我想看到一個錯誤,如「無法重新啓動服務」。

任何建議表示讚賞。 感謝

internal static void CheckToRestartService() 
    { 
     DialogResult result = MessageBox.Show(CommonCodeClass.resartServiceMessage, "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); 
     if (result == DialogResult.Yes) 
     { 
      CommonCodeClass.RestartService(CommonCodeClass.serviceName, 60000); 
     } 
    } 

回答

1

他們是否會拋出不同的例外?如果他們這樣做,你可以使用異常過濾:

private void btnApply_Click(object sender, EventArgs e) 
{ 
    try 
    {    
     applyChangesAndCheckRestartService(); 
    } 

    // catch service start exceptions 
    catch (InvalidOperationException ioex) 
    { 
     // display message that couldn't start service 
    } 

    // catch rest 
    catch (Exception ex) 
    { 
     MessageBox.Show("Error loading page.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    } 
} 

UPDATE這是假設你調用像ServiceController.Start()會拋出失敗InvalidOperationException,你可以很容易地在自己的錯誤條件拋出這個自己或創建自己的自定義異常。

if (/* service didn't start */) 
{ 
    throw new InvalidOperationException("Could not start service."); 
} 
+0

參考資料我喜歡你的想法,但如何將ioex並抓住它。我編輯了我的答案以顯示重新啓動服務功能。請看一看。感謝 – user175084

+0

@ user175084對不起,我想我失去了在您的評論的東西。這是如何*扔* ioex? –

+0

燁抱歉..想怎麼爲它創建一個單獨的異常? – user175084

1

來處理這種情況,最快的方式,當你的內部方法有人失敗,趕在btnApply_Click消息被拋出異常。

MessageBox.Show(ex.Message, "Error", .....); 

的右端的方法是創建自己的異常類型和方法中,如果有失敗狀態拋出自己的異常。例如創建一個這樣

public class RestartServiceException : Exception 
{ 
    public RestartServiceException(string message) 
     : base(message) 
    { 
    } 
    // You could also write other constructors with different parameters and use internal logic 
    // to process your error message 
} 

一類,然後,使用類的一個實例,當失敗的CheckToRestartService方法

if(fail == true) 
     throw new RestartServiceException("The service could not be started because ....."); 
+0

這是真的好!!!!這可能是工作完美... – user175084

+0

我想這樣來做。有沒有樣本或示例頁面,其中包含上述步驟? – user175084

+1

這並不複雜。您需要創建一個從基類Exception派生的類。然後你可以實現你的方法或內部變量來表示你的信息和錯誤條件。在此[鏈接](http://msdn.microsoft.com/en-us/library/ms229014.aspx),你可以找到從MSDN – Steve

1

您可能需要

  • Catch中的條件出現applyChangesAndCheckRestartService的例外
  • 或者您可以通過enum f。所謂RestartStatus

    enum RestartStatus{success, unableToRestart, unableToApplySettings}; 
    
    RestartStatus status = RestartStatus.success; 
    applyChangesAndCheckRestartService(status); 
    if(status != RestartStatus.success) //.... 
    
    private void applyChangesAndCheckRestartService(out RestartStatus status) 
    { 
        // set the status variable accordingly 
    } 
    

第三種方法是使用custom exceptions,你可以單獨趕上。

1

嗯,也許你只需要與單獨的try/catch塊來包裝不同的功能:

try { 
     if (!ApplyChangesForSettings()) 
      return; 
    } 
    catch (Exception ex) { 
     MessageBox.Show("Error loading page.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    } 

    if (svrPortNo != tbSvrPortNo.Text) {  
     try {   
      CommonCodeClass.CheckToRestartService(); 
     } 
     catch (Exception ex) { 
      MessageBox.Show("Unable to restart services.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
    } 

或者,你可以考慮捕捉不同類型的異常,如果他們把不同的類型:

string errmsg = string.empty; 
try { 
    DoSomething(); 
} 
catch (FooException) { 
    errmsg = "There was a Foo error"; 
} 
catch (WidgetException) { 
    errmsg = "There was a problem with a Widget"; 
} 
catch (Exception ex) { 
    errmsg = "General problem: " + ex.Message; 
} 

if (!string.IsNullOrEmpty(errmsg)) 
    MessageBox.Show(errmsg); 

參見:

相關問題