C#类中的异常处理
2008年 我开发了下面的类。我必须从网络服务器获取余额。完成后,它将返回到我的主应用程序,并显示结果 但是,有时web服务器由于未知原因而失败。可能是高流量或其他原因。然而,我还没有在我的类中实现任何异常处理。因为使用此功能的应用程序会处理异常 但是,客户端已确认,当web服务器发生故障时,它将显示一个未处理的异常对话框。然后他们必须单击“继续”以继续使用我的应用程序 因此,下面我不确定是否应该在类中实现异常处理。然而,我不明白为什么我的应用程序中没有捕捉到异常,如下所示 非常感谢您的建议,或者如果您发现任何其他错误C#类中的异常处理,c#,exception,class,C#,Exception,Class,2008年 我开发了下面的类。我必须从网络服务器获取余额。完成后,它将返回到我的主应用程序,并显示结果 但是,有时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;
}
}
}
private void OnGetBalanceCompleted(对象发送方,SIPPhoneLibraryEventArgs e)
{
尝试
{
//如果余额以“null”开头,则尝试获取余额时出错。
如果(例如,余额为空)
{
statusDisplay1.CurrentBalance=CATWinSIP\u MsgStrings.BalanceError;
}
其他的
{
//显示当前余额并四舍五入至小数点后2位。
statusDisplay1.CurrentBalance=Math.Round(Convert.ToDecimal(e.Balance),2.ToString();
//如果余额为零,则在状态消息中显示
if(十进制解析(e.Balance)==0)
{
this.statusDisplay1.CallStatus=“零余额”;
}
}
//删除不再需要的事件
siplibrary.GetBalanceCompletedEvent-=新事件处理程序(OnGetBalanceCompleted);
}
捕获(WebException ex)
{
MessageBox.Show(例如Message);
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message);
}
}
//所有导入函数的控件库
公共类余额:IDisposable
{
//建造师
网络客户端;
公共收支()
{
使用(wc=new-WebClient())
{
//为已更改的进度创建事件处理程序并下载已完成的事件
wc.DownloadProgressChanged+=新的DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
wc.DownloadStringCompleted+=新的DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
}
}
~Balance()
{
本.处置(假);
}
//事件处理程序和处理事件的方法
公共事件处理程序GetBalanceCompletedEvent;
//引发事件的方法
已完成公共作废OnGetBalance(SIPPhoneLibraryEventArgs e)
{
如果(GetBalanceCompletedEvent!=null)
{
GetBalanceCompletedEvent(本,e);
}
}
//获取已登录用户的当前余额。
//如果从服务器返回的余额为空,则向用户显示错误。
//如果数据库已停止或服务器已关闭,则可能会出现Null。
公共void GetBalance(字符串sipUsername)
{
//从用户名中删除下划线(41;,因为这不是获得余额所必需的。
sipUsername=sipUsername.Remove(0,1);
string strURL=string.Format(“http://xxx.xxx.xx.xx:xx/voipbilling/servlet/advcomm.voipbilling.GetBalance?CustomerID={0}(用户名);
//仅当网络客户端不忙时下载。
如果(!wc.IsBusy)
{
//睡眠1/2秒,让服务器有时间更新余额。
系统.线程.线程.睡眠(500);
//下载当前余额。
DownloadStringAsync(新Uri(strURL));
}
其他的
{
System.Windows.Forms.MessageBox.Show(“忙,请重试”);
}
}
//下载完成后返回并显示余额
void wc_DownloadStringCompleted(对象发送方,DownloadStringCompletedEventArgs e)
{
//将结果传递给事件处理程序
此.OnGetBalanceCompleted(新SIPPhoneLibraryEventArgs(e.Result));
}
//进展平衡状态。
void wc_DownloadProgressChanged(对象发送方,DownloadProgressChangedEventArgs e)
{
//将详细信息写入屏幕。
Console.WriteLine(例如TotalBytesToReceive);
控制台。写入线(例如接收到的字节数);
控制台写入线(如百分比);
}
//处理平衡对象
公共空间处置()
{
处置(真实);
总干事(本);
}
//删除事件处理程序
私有bool isDisposed=false;
私有无效处置(bool处置)
{
如果(!this.isDisposed)
{
如果(处置)
{
wc.DownloadProgressChanged-=新的DownloadProgressChangedEventHandler(wc\u DownloadProgressChanged);
wc.DownloadStringCompleted-=新的DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
wc.Dispose();
}
isDisposed=true;
}
}
}
消息
属性。通过仅显示消息
属性,您将丢弃所有这些信息。改用ex.ToString()
MessageBox.Show
消息
属性。只显示消息就是在丢弃所有这些信息