1.在Web.config中配置添加应用程序异常处理的页面
<customErrors mode="On" defaultRedirect="Error.aspx" />
Error.aspx中:
private void Page_Load(object sender, System.EventArgs e)
{
string xx=session["username"].tostring();
Exception ex=Server.GetLastError();
Session[ "error "]=ex.Message;
Server.ClearError();
}
2.在客户端捕获WebService抛出的自定义异常
比如自定义异常是处理ORACLE抛出的异常:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using xxx.xxx.xxx.CustomException;
namespace xxx.xxx.Common.Utility
{
/// <summary>
/// 类 编 号:M05_OracleRaiseError
/// 类 名 称:OracleRaiseError
/// 编码作者:xxx
/// 完成日期:2006-12-8
/// 内容摘要:数据库异常。
/// </summary>
[Serializable]
public class OracleRaiseError : BusinessException
{
/// <summary>
/// 领料单状态不正确。
/// </summary>
public OracleRaiseError()
: base("数据库操作错误")
{
}
//+-------------------------------------------------------------------------------------------
/// <summary>
/// 用错误信息创建对象。
/// </summary>
/// <param name="message">描述异常的信息。</param>
public OracleRaiseError(string message)
: base(message)
{
}
//+-------------------------------------------------------------------------------------------
/// <summary>
/// 用错误信息和导致异常的内部异常来创建对象。
/// </summary>
/// <param name="message">描述异常的信息。</param>
/// <param name="inner">导致目前异常的异常对象。</param>
public OracleRaiseError(string message, Exception inner)
: base(message, inner)
{
}
//+-------------------------------------------------------------------------------------------
/// <summary>
/// 用于将异常序列化的类。
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
public OracleRaiseError(SerializationInfo info, StreamingContext context)
: base(info, context)
{ }
}
}
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using xxx.xxx.xxx.CustomException;
namespace xxx.xxx.Common.Utility
{
/// <summary>
/// 类 编 号:M05_OracleRaiseError
/// 类 名 称:OracleRaiseError
/// 编码作者:xxx
/// 完成日期:2006-12-8
/// 内容摘要:数据库异常。
/// </summary>
[Serializable]
public class OracleRaiseError : BusinessException
{
/// <summary>
/// 领料单状态不正确。
/// </summary>
public OracleRaiseError()
: base("数据库操作错误")
{
}
//+-------------------------------------------------------------------------------------------
/// <summary>
/// 用错误信息创建对象。
/// </summary>
/// <param name="message">描述异常的信息。</param>
public OracleRaiseError(string message)
: base(message)
{
}
//+-------------------------------------------------------------------------------------------
/// <summary>
/// 用错误信息和导致异常的内部异常来创建对象。
/// </summary>
/// <param name="message">描述异常的信息。</param>
/// <param name="inner">导致目前异常的异常对象。</param>
public OracleRaiseError(string message, Exception inner)
: base(message, inner)
{
}
//+-------------------------------------------------------------------------------------------
/// <summary>
/// 用于将异常序列化的类。
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
public OracleRaiseError(SerializationInfo info, StreamingContext context)
: base(info, context)
{ }
}
}
在业务层捕获错误:
代码
/// <summary>
/// 内容摘要:查询数据
/// </summary>
/// <param name="index">查询序号</param>
/// <param name="ht">查询参数</param>
/// <returns></returns>
public DataSet QueryBasicInfo(int index, Hashtable ht)
{
try
{
DataSet ds = mDao.QueryBasicInfo(index, ht);
return ds;
}
catch (OracleException oracleEx)
{
throw new OracleRaiseError(oracleEx.Message);
}
catch (Exception ex)
{
throw this.HandleException(ex);
}
}
/// 内容摘要:查询数据
/// </summary>
/// <param name="index">查询序号</param>
/// <param name="ht">查询参数</param>
/// <returns></returns>
public DataSet QueryBasicInfo(int index, Hashtable ht)
{
try
{
DataSet ds = mDao.QueryBasicInfo(index, ht);
return ds;
}
catch (OracleException oracleEx)
{
throw new OracleRaiseError(oracleEx.Message);
}
catch (Exception ex)
{
throw this.HandleException(ex);
}
}
在WebService中调用业务层的方法,并捕获错误:
代码
/// <summary>
/// 内容摘要:查询数据
/// </summary>
/// <param name="index">查询序号</param>
/// <param name="entries">查询参数</param>
/// <returns>查询数据</returns>
[WebMethod]
public DataSet QueryBasicInfo(int index, DictionaryEntry[] entries)
{
Hashtable ht = GetHashtable(entries);
try
{
return fittingDs.QueryBasicInfo(index, ht);
}
catch (OracleRaiseError ore)
{
throw new SoapException(FuntionLib.ParseOracleError(ore.Message), SoapException.ServerFaultCode);
}
catch (Exception ex)
{
throw ex;
}
}
/// 内容摘要:查询数据
/// </summary>
/// <param name="index">查询序号</param>
/// <param name="entries">查询参数</param>
/// <returns>查询数据</returns>
[WebMethod]
public DataSet QueryBasicInfo(int index, DictionaryEntry[] entries)
{
Hashtable ht = GetHashtable(entries);
try
{
return fittingDs.QueryBasicInfo(index, ht);
}
catch (OracleRaiseError ore)
{
throw new SoapException(FuntionLib.ParseOracleError(ore.Message), SoapException.ServerFaultCode);
}
catch (Exception ex)
{
throw ex;
}
}
其中,PareOracleError是解析Oracle抛出的异常:
代码
# region 解析ORACLE存储过程抛出的错误
/// <summary>
/// 解析ORACLE存储过程抛出的错误,
/// 根据特殊字符 %% 分解中英文
/// </summary>
/// <param name="errorMessage"></param>
/// <returns></returns>
public static string ParseOracleError(string errorMessage)
{
int startPoint = errorMessage.IndexOf(": ") + 2;
int endPoint = errorMessage.IndexOf("\n");
errorMessage = errorMessage.Substring(startPoint, endPoint - startPoint);
if (errorMessage.IndexOf("##") > 0)
{
if (System.Threading.Thread.CurrentThread.CurrentUICulture.Name == "en-US")
{
errorMessage = errorMessage.Substring(errorMessage.IndexOf("##") + 2);
}
else
{
errorMessage = errorMessage.Substring(0, errorMessage.IndexOf("##"));
}
}
return errorMessage;
}
# endregion
/// <summary>
/// 解析ORACLE存储过程抛出的错误,
/// 根据特殊字符 %% 分解中英文
/// </summary>
/// <param name="errorMessage"></param>
/// <returns></returns>
public static string ParseOracleError(string errorMessage)
{
int startPoint = errorMessage.IndexOf(": ") + 2;
int endPoint = errorMessage.IndexOf("\n");
errorMessage = errorMessage.Substring(startPoint, endPoint - startPoint);
if (errorMessage.IndexOf("##") > 0)
{
if (System.Threading.Thread.CurrentThread.CurrentUICulture.Name == "en-US")
{
errorMessage = errorMessage.Substring(errorMessage.IndexOf("##") + 2);
}
else
{
errorMessage = errorMessage.Substring(0, errorMessage.IndexOf("##"));
}
}
return errorMessage;
}
# endregion
在WINFORM客户端调用WEBSERVICE,并捕获异常:
代码
try
{
Hashtable ht = new Hashtable();
ht.Add("CardNo", txtCardNo.Text.Trim());
IQueryDs mDs = new QueryDs ();
DataSet ds = mDs.QueryBasicInfo(7, ht);
.
.
.
}
catch (SoapException ore)
{
PDAMessageBoxWrapper.ShowInfo(PDAFuntionLib.ParseSoapError(ore.Message));
// 初始化窗口
InitForm();
}
catch
{
PDAMessageBoxWrapper.ShowError("加载数据时出错!");
}
{
Hashtable ht = new Hashtable();
ht.Add("CardNo", txtCardNo.Text.Trim());
IQueryDs mDs = new QueryDs ();
DataSet ds = mDs.QueryBasicInfo(7, ht);
.
.
.
}
catch (SoapException ore)
{
PDAMessageBoxWrapper.ShowInfo(PDAFuntionLib.ParseSoapError(ore.Message));
// 初始化窗口
InitForm();
}
catch
{
PDAMessageBoxWrapper.ShowError("加载数据时出错!");
}
其中,PareSoapError的方法是:
代码
# region 解析ORACLE存储过程抛出的错误
/// <summary>
/// 解析SoapException抛出的错误,
/// 根据特殊字符 %% 分解中英文
/// </summary>
/// <param name="errorMessage"></param>
/// <returns></returns>
public static string ParseSoapError(string errorMessage)
{
int startPoint = errorMessage.IndexOf(": ") + 2;
int endPoint = errorMessage.IndexOf("\n");
errorMessage = errorMessage.Substring(startPoint, endPoint - startPoint);
return errorMessage;
}
#endregion
/// <summary>
/// 解析SoapException抛出的错误,
/// 根据特殊字符 %% 分解中英文
/// </summary>
/// <param name="errorMessage"></param>
/// <returns></returns>
public static string ParseSoapError(string errorMessage)
{
int startPoint = errorMessage.IndexOf(": ") + 2;
int endPoint = errorMessage.IndexOf("\n");
errorMessage = errorMessage.Substring(startPoint, endPoint - startPoint);
return errorMessage;
}
#endregion