时间:2016-02-24 18:50 来源: 我爱IT技术网 作者:佚名
欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【错误处理】,下面是详细的分享!
错误处理
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Web;
using System.Web.Caching;
using System.Collections;
namespace MeetingProxy.MeetingException

{
/// <summary>
/// 错误码的描述
/// </summary>
public class ErrProcedure
{
private static Hashtable errMessages = new Hashtable();
public static Hashtable GetErrMessages()
{
if (CommonCache.Get("ErrMessage") as Hashtable == null)
{
string path = null;
HttpContext context = HttpContext.Current;
if (context != null)
path = context.Server.MapPath("~ErrMessage.xml");
else
path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ErrMessage.xml");
XmlDocument xdoc = new XmlDocument();
xdoc.Load(path);
foreach (XmlNode child in xdoc.LastChild)
{
errMessages.Add(int.Parse(child.LastChild.InnerText), child.FirstChild.InnerText);
}
CacheDependency cd = new CacheDependency(path);
CommonCache.Max("ErrMessage", errMessages,cd);
return errMessages;
}
else
{
return CommonCache.Get("ErrMessage") as Hashtable;
}
}


}
}
二、只返回错误码,客户端去解析错误码,这样提供了,最大的灵活性。实现:服务器省去了错误码的解析。
异常抛出方式:
throw new MeetingBaseException(108);
客户端实现:下面是用到资源文件的例子,如果只有一种资源,就可以用ErrProcedure处理。
因为全局资源被编译为资源类,而错误码是数字,不能做标志符,因此把他做简单处理,正数前加“R”,负数加“R_”,修改后类似(简体资源):
<data name="R303" xml:space="preserve">
<value>用户名已经存在</value>
</data><data name="R_500" xml:space="preserve">
<value>系统异常</value>
</data> 处理方式:
public static string GetErrInfo(int errCode)
{
string resourceKey;
if (errCode >= 0)
{
resourceKey = "R" + errCode.ToString();
}
else
{
errCode = errCode * -1;
resourceKey = "R_" + errCode.ToString();
}
Type t = typeof(Resources.Login);
PropertyInfo pi = t.GetProperty(resourceKey);
return pi.GetValue(null, null).ToString();

}
此处用反射简化了很多,见http://www.cnblogs.com/bluewater/archive/2006/09/08/498660.html
我认为第二种方式在大多数情况下都比较好,如果是很小的项目,第一种方式会简单些。
webservice异常处理:
在webservice中抛出异常,让客户端去解析是很困难的,因为客户端可能是js,php等,对ws的封装很差。因此我认为好一些的异常处理应该这样:
返回结果分两种类型,一种是只包含简单的错误码:
using System;
using System.Collections.Generic;
using System.Text;
namespace YahooBase.Result

{
/// <summary>
/// 普通返回值
/// </summary>
public class SoapResult
{
private int errCode;
public SoapResult()
{ }
public SoapResult(int errCode)
{
this.errCode = errCode;
}
public int ErrCode
{
get
{ return errCode; }
set
{ errCode = value; }
}
}
}
web服务方法实现:[WebMethod()]
public SoapResult FreeConfrence()

{
try

{
mc.FreeConfrence();
return new SoapResult(0);
}
catch (MeetingBaseException me)

{
return new SoapResult(me.ErrCode);

}
catch (ApplicationException ae)

{
Log4net.log.Error(ae);
return new SoapResult(-500);
}

catch (Exception e)

{
if (e.InnerException != null)

{
Log4net.log.Error(e.InnerException);
}
Log4net.log.Error(e);
return new SoapResult(-500);
}
}
另一种返回复杂结果:
先定义返回值类型如,HistoryFeeResult
using System;
using System.Collections.Generic;
using System.Text;

namespace YahooBase.Result


{

/// <summary>
/// 查询历史帐单信息
/// </summary>
public class HistoryFeeResult:SoapResult

{
public HistoryFeeResult(int errCode):base(errCode)

{}
public HistoryFeeResult()

{}
public MonthFee[] MonthFeeList;
}
public class MonthFee

{
public string MonthName;
public Category[] CategoryList;

}
public class Category

{
public Category(decimal fee, string name)

{
Fee = fee;
Name = name;
}
public Category()

{ }
public decimal Fee;
public string Name;

}
}
web方法实现:
实现方式和第一种一样,只是如果有自定义异常,则调用HistoryFeeResult的只有错误码的构造函数。
如果调用成功会返回(Post方式):
<?xml version="1.0" encoding="utf-8" ?>
- <HistoryFeeResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<ErrCode>0</ErrCode>
- <MonthFeeList>
- <MonthFee>
<MonthName>三月</MonthName>
- <CategoryList>
- <Category>
<Fee>44.6</Fee>
<Name>市话</Name>
 

public SoapResult FreeConfrence()

{
try
{
mc.FreeConfrence();
return new SoapResult(0);
}
catch (MeetingBaseException me)
{
return new SoapResult(me.ErrCode);
}
catch (ApplicationException ae)
{
Log4net.log.Error(ae);
return new SoapResult(-500);
}
catch (Exception e)
{
if (e.InnerException != null)
{
Log4net.log.Error(e.InnerException);
}
Log4net.log.Error(e);
return new SoapResult(-500);
}
}
另一种返回复杂结果:先定义返回值类型如,HistoryFeeResult
using System;
using System.Collections.Generic;
using System.Text;
namespace YahooBase.Result

{
/// <summary>
/// 查询历史帐单信息
/// </summary>
public class HistoryFeeResult:SoapResult
{
public HistoryFeeResult(int errCode):base(errCode)
{}
public HistoryFeeResult()
{}
public MonthFee[] MonthFeeList;
}
public class MonthFee
{
public string MonthName;
public Category[] CategoryList;
}
public class Category
{
public Category(decimal fee, string name)
{
Fee = fee;
Name = name;
}
public Category()
{ }
public decimal Fee;
public string Name;
}
}
web方法实现:实现方式和第一种一样,只是如果有自定义异常,则调用HistoryFeeResult的只有错误码的构造函数。
如果调用成功会返回(Post方式):
<?xml version="1.0" encoding="utf-8" ?>
- <HistoryFeeResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<ErrCode>0</ErrCode>
- <MonthFeeList>
- <MonthFee>
<MonthName>三月</MonthName>
- <CategoryList>
- <Category>
<Fee>44.6</Fee>
<Name>市话</Name>
 
以上所分享的是关于错误处理,下面是编辑为你推荐的有价值的用户互动:
相关问题:记帐错误的处理
答:发现记账错误时,根据账簿错误的具体情况,可采取三种不同的更正方法: (1)划线更正法。凡在结账前发现账簿记录中文字或数字错误时,可采用此法。更正时,在账簿中错误文字或数字上划一红线,以示注销。划线时,要划去错误数字的整个数码,不能... >>详细
相关问题:js中关于错误处理的问题
答:("错误名称: " + err.name+" ---> "); ("错误信息: " + err.message+" ---> "); 我常用到的就这两个 >>详细
相关问题:jsp处理错误
答:JSPG0225E: 静态包含的文件 /icbc/newenperbank/includes/token.jsp 中的第 22 行发生错误 >>详细
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
