2009-04-28 72 views
1

C#2008C#異常處理類

我開發了下面的類。我必須從Web服務器獲取餘額。一旦完成,它會回調我的主應用程序的結果。

但是,有時Web服務器出於某種未知原因失敗。可能是大量的流量或其他東西。但是,我沒有在我的課上實施任何異常處理。由於使用此應用程序處理異常。

但是,客戶端已經確認,當Web服務器確實失敗時,它會顯示未處理的異常對話框。然後他們必須點擊繼續繼續使用我的應用程序。

所以下面我不確定是否應該在我的類中實現異常處理。不過,我很困惑,爲什麼這個例外沒有在我的應用程序中被捕獲,如下所示。

的任何建議非常感謝,或者如果你看到別的錯誤,

private void OnGetBalanceCompleted(object sender, SIPPhoneLibraryEventArgs e) 
    { 
     try 
     { 
      //If the balance starts with 'null' there has been an error trying to get the balance. 
      if (e.Balance.StartsWith("null")) 
      { 
       statusDisplay1.CurrentBalance = CATWinSIP_MsgStrings.BalanceError; 
      } 
      else 
      { 
       // Display the current balance and round to 2 decimal places. 
       statusDisplay1.CurrentBalance = Math.Round(Convert.ToDecimal(e.Balance), 2).ToString(); 

       //If the balance is zero display in the status message 
       if (decimal.Parse(e.Balance) == 0) 
       { 
        this.statusDisplay1.CallStatus = "Zero Balance"; 
       } 
      } 
      //Remove the event as no longer needed 
      siplibrary.GetBalanceCompletedEvent -= new EventHandler<SIPPhoneLibraryEventArgs>(OnGetBalanceCompleted); 
     } 
     catch (WebException ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 




//Control library for all importing functions 
public class Balance : IDisposable 
{ 
    //Constructor 
    WebClient wc; 
    public Balance() 
    { 
     using (wc = new WebClient()) 
     { 
      //Create event handler for the progress changed and download completed events 
      wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged); 
      wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted); 
     } 
    } 

    ~Balance() 
    { 
     this.Dispose(false); 
    } 

    //Event handler and the method that handlers the event 
    public EventHandler<SIPPhoneLibraryEventArgs> GetBalanceCompletedEvent; 

    //The method that raises the event 
    public void OnGetBalanceCompleted(SIPPhoneLibraryEventArgs e) 
    { 
     if (GetBalanceCompletedEvent != null) 
     { 
      GetBalanceCompletedEvent(this, e); 
     } 
    } 

    //Get the current balance for the user that is logged in. 
    //If the balance returned from the server is NULL display error to the user. 
    //Null could occur if the DB has been stopped or the server is down.  
    public void GetBalance(string sipUsername) 
    { 
     //Remove the underscore (_) from the username, as this is not needed to get the balance. 
     sipUsername = sipUsername.Remove(0, 1); 

     string strURL = string.Format("http://xxx.xxx.xx.xx:xx/voipbilling/servlet/advcomm.voipbilling.GetBalance?CustomerID={0}", sipUsername); 

     //Download only when the webclient is not busy. 
     if (!wc.IsBusy) 
     { 
      // Sleep for 1/2 second to give the server time to update the balance. 
      System.Threading.Thread.Sleep(500); 
      // Download the current balance. 
      wc.DownloadStringAsync(new Uri(strURL)); 
     } 
     else 
     { 
      System.Windows.Forms.MessageBox.Show("Busy please try again"); 
     } 
    } 

    //return and display the balance after the download has fully completed 
    void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
    { 
     //Pass the result to the event handler 
     this.OnGetBalanceCompleted(new SIPPhoneLibraryEventArgs(e.Result)); 
    } 

    //Progress state of balance. 
    void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) 
    { 
     //Write the details to the screen. 
     Console.WriteLine(e.TotalBytesToReceive); 
     Console.WriteLine(e.BytesReceived); 
     Console.WriteLine(e.ProgressPercentage); 
    } 


    //Dispose of the balance object 
    public void Dispose() 
    { 
     Dispose(true); 

     GC.SuppressFinalize(this); 
    } 

    //Remove the event handlers 
    private bool isDisposed = false; 
    private void Dispose(bool disposing) 
    { 
     if (!this.isDisposed) 
     { 
      if (disposing) 
      { 
       wc.DownloadProgressChanged -= new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged); 
       wc.DownloadStringCompleted -= new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted); 

       wc.Dispose(); 
      }    
      isDisposed = true; 
     } 
    } 
} 

回答

2

看來您只是在OnGetBalanceCompleted事件上捕獲異常,而不是在獲取餘額的過程中。

當讀取出現錯誤時,OnGetBalanceCompleted甚至沒有被調用,這就是爲什麼你的異常處理程序沒有被調用。

2
  1. 沒有在異常不僅僅是它Message屬性的詳細信息。您只需顯示Message屬性即可丟棄所有這些信息。改爲使用ex.ToString()
  2. 您發佈的代碼是用戶界面的一部分嗎?如果沒有,那麼它就沒有任何業務知道用戶界面的任何信息。特別是,它不應該使用MessageBox.Show
  3. 我想刪除所有UI的東西,而是引發一個事件。調用者將聽取該事件並執行任何UI工作。