日期:2014-05-17  浏览次数:20405 次

关于异常处理
C# code

public static User XmlToUser(string xml)
        {
            XmlDocument doc = new XmlDocument();
            User user = new User();
            try
            {
                doc.LoadXml(xml);
                user.IDNo = (doc.SelectSingleNode("Request/IDNo").InnerText == null) ? "" : doc.SelectSingleNode("Request/IDNo").InnerText;
                user.IDType = (doc.SelectSingleNode("Request/IDType").InnerText == null) ? "" : doc.SelectSingleNode("Request/IDType").InnerText; ;
                user.MarrigeStatus = (doc.SelectSingleNode("Request/MarrigeStatus").InnerText == null) ? "" : doc.SelectSingleNode("Request/MarrigeStatus").InnerText;
                user.Mobile = (doc.SelectSingleNode("Request/Mobile").InnerText == null) ? "" : doc.SelectSingleNode("Request/Mobile").InnerText;
                user.Name = (doc.SelectSingleNode("Request/Name").InnerText == null) ? "" : doc.SelectSingleNode("Request/Name").InnerText;
                user.Sex = (doc.SelectSingleNode("Request/Sex").InnerText == null) ? "" : doc.SelectSingleNode("Request/Sex").InnerText;
                user.TelephoneNo = (doc.SelectSingleNode("Request/TelephoneNo").InnerText == null) ? "" : doc.SelectSingleNode("Request/TelephoneNo").InnerText;
                user.Uid = (doc.SelectSingleNode("Request/Uid").InnerText == null) ? 0 : Convert.ToInt32(doc.SelectSingleNode("Request/Uid").InnerText);
                user.Zip = (doc.SelectSingleNode("Request/Zip").InnerText == null) ? "" : doc.SelectSingleNode("Request/Zip").InnerText;
                user.DOB = (doc.SelectSingleNode("Request/DOB").InnerText == null) ? DateTime.Now : DateTime.Parse(doc.SelectSingleNode("Request/DOB").InnerText);
                user.CreateDate = (doc.SelectSingleNode("Request/CreateDate").InnerText == null) ? DateTime.Now : DateTime.Parse(doc.SelectSingleNode("Request/CreateDate").InnerText);
                user.Address = (doc.SelectSingleNode("Request/Address").InnerText == null) ? "" : doc.SelectSingleNode("Request/Address").InnerText;
            }
            catch
            {
                throw;
            }
            return user;
        }


本人写了这么个方法,本人用throw抛出异常,然后在WebService中调用这个方法,想在WebService中处理这个异常然后将异常信息写入日志,大家教教我怎么做,本人很菜,还有想写个异常类,来集中处理自己抛出的异常,问哈大家如何写这个异常类,在这里先祝各位工作顺利

------解决方案--------------------
给你看看我写的异常记录吧

异常信息还是认真的记录下来的好

C# code

  catch (Exception ex)
        {
            Log.Write("\r\n----------------------------\r\n");
            Log.Write("提交客户电子注册错误!" + DateTime.Now + "错误描述" + ex.ToString() + "\r\n");
            Log.Write("----------------------------\r\n");
        }

------解决方案--------------------
呵呵 你可以直接用log4net

web.config
XML code
<?xml version="1.0" encoding="utf-8" ?>  
<configuration>  
 <configSections>  
  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />  
 </configSections>  
 <log4net>  
  <logger name="logerror">  
   <level value="ERROR" />  
   <appender-ref ref="ErrorAppender" />  
  </logger>  
  <logger name="loginfo">  
   <level value="INFO" />  
   <appender-ref ref="InfoAppender" />  
  </logger>  
  <ap