我用下面这个类来抓取信息, 但一旦有异常的时候程序就停止的
我想添加委托或事件的来处理异常 将异常记录在数据库中另外还有没有其他方案
public static class WebRequestHelper
    {
        public static string GetHtmlFromUrl(string url, Encoding encoding)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            StreamReader reader = null;
            Stream dataStream = null;
            string strHtml = null;
            try
            {
                // Create a request for the URL. 
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Timeout = 1000 * 10; //毫秒 * 秒
                request.AllowAutoRedirect = false;
                // If required by the server, set the credentials.
                request.Credentials = CredentialCache.DefaultCredentials;                // Get the response.
                response = (HttpWebResponse)request.GetResponse();                if (response.StatusCode == HttpStatusCode.OK)
                {
                    // Get the stream containing content returned by the server.
                    dataStream = response.GetResponseStream();
                    // Open the stream using a StreamReader for easy access.
                    reader = new StreamReader(dataStream, encoding);
                    // Read the content.
                    strHtml = reader.ReadToEnd();
                }
            }
            catch (WebException we) {
                if (we.Status==WebExceptionStatus.Timeout)
                {
                    return null;
                }
                throw we;//不要throw 记录在数据库中,怎么记录 通过委托事件在外面实现 怎么修改好呢 谢谢
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (dataStream != null)
                {
                    dataStream.Close();
                }
                if (response != null)
                {
                    response.Close();
                }                request = null;
            }            return strHtml;
        }
    }

解决方案 »

  1.   

    就直接抛出异常就行,在外层try catch一下将得到的异常消息写入数据库,比用委托简单
      

  2.   

    你在catch 中将异常屏蔽掉就可以了。
      

  3.   

    MS还有个包装类叫 WebClient 当然try...catch还是在调用它的方法里做比较好。
      

  4.   

    你的代码确实跟面向对象编程没有关系。面向对象编程是指某个class中有个属性,或者某个方法的参数,等等,定义为类型或者接口T,而其实际运行时是使用T的实例还是T的子类的实例,都是无所谓的,是多态的。这才是面向对象,它最起码要new出来这个对象的实例对象。而你的就是函数库。如果需要扩展出一个记录方法delegate,可以简单地这样修改:public static class WebRequestHelper
        {
            public static Action<Exception> LogExceptionInfoToDB;        public static string GetHtmlFromUrl(string url, Encoding encoding)
            {
               try
               {
                   return rGetHtmlFromUrl(string url, Encoding encoding);
                }
                catch(Exception e)
                {
                   if(LogExceptionInfoToDB!=null)
                        LogExceptionInfoToDB(e);  //将异常记录在数据库中
                   throw e;
                }
            }        private static string rGetHtmlFromUrl(string url, Encoding encoding)
            {
                HttpWebRequest request = null;
                HttpWebResponse response = null;
                StreamReader reader = null;
                Stream dataStream = null;
                string strHtml = null;
    .............