下面的代码中如果连接服务器出现异常就会抛出异常throw new FTPException("Can not Connect the Server");
但是程序也就在此中断了,请问怎么才能避免这种情况?throw是不是只有在测试中有用呢?
public virtual void Connect()
{
lock (clientLock)
{
bool loggedIn = false;
try
{
if (LocalDirectory==null)
LocalDirectory = Directory.GetCurrentDirectory();
if (ServerAddress == null)
throw new FTPException("ServerAddress not set");
OnConnecting();
ActiveClient.Connect();
log.Debug("Connected to " + ServerAddress + " (instance=" + instanceNumber + ")");
OnConnected(true);
loggedIn = PerformAutoLogin();
}
catch
{//此处进行了修改
OnConnected(IsConnected);
if (!IsConnected)
OnClosed();
throw new FTPException("Can not Connect the Server"); }
if (loggedIn)
PostLogin();
}
但是程序也就在此中断了,请问怎么才能避免这种情况?throw是不是只有在测试中有用呢?
public virtual void Connect()
{
lock (clientLock)
{
bool loggedIn = false;
try
{
if (LocalDirectory==null)
LocalDirectory = Directory.GetCurrentDirectory();
if (ServerAddress == null)
throw new FTPException("ServerAddress not set");
OnConnecting();
ActiveClient.Connect();
log.Debug("Connected to " + ServerAddress + " (instance=" + instanceNumber + ")");
OnConnected(true);
loggedIn = PerformAutoLogin();
}
catch
{//此处进行了修改
OnConnected(IsConnected);
if (!IsConnected)
OnClosed();
throw new FTPException("Can not Connect the Server"); }
if (loggedIn)
PostLogin();
}
建议你看一下http://www.cnblogs.com/hejunrex/archive/2010/01/20/1652084.html
不知道把throw new FTPException("Can not Connect the Server"); 去掉会有什么影响
catch
{//此处进行了修改
OnConnected(IsConnected);
if (!IsConnected)
OnClosed();
throw new FTPException("Can not Connect the Server");
}
中已经捕获到了异常,那么我就不需要再throw new FTPException("Can not Connect the Server");
如果在catch中捕获怎么写呢?
{
throw new Exception(e.Message);
}
处理关键在于函数调用的约定:谁抛出异常,谁处理异常。