2009-04-28 73 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工作。