2009-04-28 16 views
1

C#2008クラス内の例外処理

私は以下のクラスを開発しました。私は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プロパティよりも例外ではより多くの情報があります。プロパティを表示するだけで、すべての情報を捨ててしまいます。代わりにex.ToString()を使用してください。
  2. 投稿したコードはユーザーインターフェイスの一部ですか?そうでなければ、それはユーザーインターフェースに関する何も知らないビジネスはありません。特に、MessageBox.Showを使用するべきではありません。
  3. 私はすべてのUIのものを削除して、代わりにイベントを発生させます。呼び出し元はイベントを聞いてUI作業を行います。