这是异常类的代码,如果你有空的话帮我看看是不是哪里出了错using System;using System.Web; using ZeroStock.Data; using ZeroStock.API; using ZeroStock.Config; using ZeroStock.Security; using ZeroStock.Common; using System.Runtime.InteropServices; namespace ZeroStock.Components { public class ZSException : ApplicationException { string message=string.Empty; public ZSExceptionType ExceptionType { get; set; } public SystemExceptionLogInfo ExceptionLogInfo { get; set; } private const string RESOURCE_CLASS_NAME = "Exception"; public int _authrityID = 0;
ExceptionLogInfo = new SystemExceptionLogInfo() { Category = ExceptionType, CreatedDate = DateTime.Now, LongMessage = base.GetBaseException().ToString(), ShortMessage = Message, Frequency = 1, LastOccurredDate = DateTime.Now, ExceptionHash=hash }; HttpContext context = HttpContext.Current; // This was failing when trying to access the database that we didn't have permissions to. When this is // happening, the application is first loading (LoadSiteSettings) and not all of this context information // is present. Because of this, we were getting an exception in the Exception class which defeats // the whole purpose of having this class. Adding some additional checks to ensure we don't throw an // exception in our exception constructor if (context != null && context.Request != null) { if (context.Request.UrlReferrer != null) ExceptionLogInfo.UrlReferrer = context.Request.UrlReferrer.ToString(); if (context.Request.UserAgent != null) ExceptionLogInfo.UserAgent = context.Request.UserAgent; if (context.Request.UserHostAddress != null) ExceptionLogInfo.IPAddress = context.Request.UserHostAddress; // "forumContext.Context.Request.Url != null" check was added because // , similarly to above, the Url property will be null when this method is called // from the ForumsHttpModule.ScheduledWorkCallbackEmailInterval timer callback. if (context.Request != null && context.Request.Url != null && context.Request.Url.PathAndQuery != null) ExceptionLogInfo.HttpPathAndQuery = context.Request.Url.PathAndQuery; // Added to have Log() working. The table columns that hold // all exception details doesn't support null values. In certain circumstances // adding exception details to database for thrown exception might run into an // unhandled exception: a new exception is thrown while current exception // processing is not finished (ForumsHttpModule.Application_OnError). if (context.Request != null && context.Request.UrlReferrer != null && context.Request.UrlReferrer.PathAndQuery != null) ExceptionLogInfo.UrlReferrer = context.Request.UrlReferrer.PathAndQuery; }
{
}
catch(ZSException zex)
{
throw zex;
}
看看
{
}
catch(ZSException zex)
{
throw zex;
}
得到
public class ZSException : IHttpModule我记得好像是继承IHttpModule,不是ApplicationException
HTTPMODULE才是继承于IHttpModule,
这是两个不同的类,不同的概念
public class ZSException : ApplicationException
{
public ZSException () {} public ZSException (string message) : base(message) {} public ZSException (string message, Exception e) : base(message, e) {}
}
你没初始化BASE。你标题是这样写的,ihttpmodule 无法 捕获 自定义异常 ,OK?
public ZSException(ZSExceptionType t):base (){
this.ExceptionType = t;
Init();
} /// <summary>
/// 访问拒绝异常
/// </summary>
/// <param name="authrityID">所需权限ID</param>
public ZSException(int authrityID):base()
{
ExceptionType = ZSExceptionType.AccessDenied;
_authrityID = authrityID;
}
/// <summary>
///
/// </summary>
/// <param name="t"></param>
/// <param name="msg"></param>
public ZSException(ZSExceptionType t, string msg) : base(msg)
{
this.ExceptionType = t;
this.message = msg;
Init();
}
public ZSException(ZSExceptionType t, string msg, Exception innerException)
: base(msg, innerException)
{
this.ExceptionType = t;
this.message = msg;
Init();
}
{
//错误
}
catch (Exception e)
{
throw new ZSException("未初始化.", e);
}
{
//错误
}
catch (Exception e)
{
throw new ZSException("未初始化.", e);
}这样是可以得到的
public class ZSHttpModule : IHttpModule
{void IHttpModule.Init(HttpApplication context)
{
context.Error += new EventHandler(this.Application_OnError);
} public void Application_OnError(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
ZSException zsException = context.Server.GetLastError() as ZSException; if (zsException == null)
zsException = context.Server.GetLastError().GetBaseException() as ZSException; try
{
....//重定向到自定义的错误页面 这样就不行了
void IHttpModule.Init(HttpApplication context)
{
context.Error += new EventHandler(this.Application_OnError);
}
这里用的就是你说的System.Web.HttpApplication.Error
using ZeroStock.Data;
using ZeroStock.API;
using ZeroStock.Config;
using ZeroStock.Security;
using ZeroStock.Common;
using System.Runtime.InteropServices;
namespace ZeroStock.Components
{
public class ZSException : ApplicationException
{
string message=string.Empty;
public ZSExceptionType ExceptionType { get; set; }
public SystemExceptionLogInfo ExceptionLogInfo { get; set; }
private const string RESOURCE_CLASS_NAME = "Exception";
public int _authrityID = 0;
public ZSException(ZSExceptionType t):base (){
this.ExceptionType = t;
Init();
} /// <summary>
/// 访问拒绝异常
/// </summary>
/// <param name="authrityID">所需权限ID</param>
public ZSException(int authrityID):base()
{
ExceptionType = ZSExceptionType.AccessDenied;
_authrityID = authrityID;
}
/// <summary>
///
/// </summary>
/// <param name="t"></param>
/// <param name="msg"></param>
public ZSException(ZSExceptionType t, string msg) : base(msg)
{
this.ExceptionType = t;
this.message = msg;
Init();
}
public ZSException(ZSExceptionType t, string msg, Exception innerException)
: base(msg, innerException)
{
this.ExceptionType = t;
this.message = msg;
Init();
}
public override string Message
{
get
{
switch (ExceptionType)
{
case ZSExceptionType.AccessDenied:
string authInfo = Authoritys.GetName(_authrityID);
return string.Format(
HttpContext.GetGlobalResourceObject(RESOURCE_CLASS_NAME, ExceptionType.ToString()).ToString(),
authInfo); case ZSExceptionType.ScheduleRunFailed:
return string.Format(
HttpContext.GetGlobalResourceObject(RESOURCE_CLASS_NAME, ExceptionType.ToString()).ToString(),
message);
default :
return HttpContext.GetGlobalResourceObject(RESOURCE_CLASS_NAME, ExceptionType.ToString()).ToString();
}
}
} public void Log()
{
SystemExceptionLogs oSystemException = new SystemExceptionLogs();
try
{
ExceptionLogInfo = oSystemException.InsertOrUpdate(this.ExceptionLogInfo);
if (Utils.InternetConnecting())
{
ZSSAPI.ReportException(ConfigProvider.GetClientInfo(), ExceptionLogInfo);
}
}
catch { }
} void Init()
{
try
{
int hash = (ExceptionType + this.ToString()).GetHashCode();
SystemExceptionLogs oSystemException = new SystemExceptionLogs();
ExceptionLogInfo = new SystemExceptionLogInfo()
{
Category = ExceptionType,
CreatedDate = DateTime.Now,
LongMessage = base.GetBaseException().ToString(),
ShortMessage = Message,
Frequency = 1,
LastOccurredDate = DateTime.Now,
ExceptionHash=hash
};
HttpContext context = HttpContext.Current; // This was failing when trying to access the database that we didn't have permissions to. When this is
// happening, the application is first loading (LoadSiteSettings) and not all of this context information
// is present. Because of this, we were getting an exception in the Exception class which defeats
// the whole purpose of having this class. Adding some additional checks to ensure we don't throw an
// exception in our exception constructor
if (context != null &&
context.Request != null)
{ if (context.Request.UrlReferrer != null)
ExceptionLogInfo.UrlReferrer = context.Request.UrlReferrer.ToString(); if (context.Request.UserAgent != null)
ExceptionLogInfo.UserAgent = context.Request.UserAgent; if (context.Request.UserHostAddress != null)
ExceptionLogInfo.IPAddress = context.Request.UserHostAddress; // "forumContext.Context.Request.Url != null" check was added because
// , similarly to above, the Url property will be null when this method is called
// from the ForumsHttpModule.ScheduledWorkCallbackEmailInterval timer callback.
if (context.Request != null
&& context.Request.Url != null
&& context.Request.Url.PathAndQuery != null)
ExceptionLogInfo.HttpPathAndQuery = context.Request.Url.PathAndQuery; // Added to have Log() working. The table columns that hold
// all exception details doesn't support null values. In certain circumstances
// adding exception details to database for thrown exception might run into an
// unhandled exception: a new exception is thrown while current exception
// processing is not finished (ForumsHttpModule.Application_OnError).
if (context.Request != null
&& context.Request.UrlReferrer != null
&& context.Request.UrlReferrer.PathAndQuery != null)
ExceptionLogInfo.UrlReferrer = context.Request.UrlReferrer.PathAndQuery;
}
}
catch { }
}
}
}
response.write(ex.Message.ToString())