ihttpmodule 无法 捕获 自定义异常

greatbag 2009-02-18 02:02:11
我写了个自定义异常public class ZSException : ApplicationException
在自己写的HTTPMODULE里写
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
{
....//重定向到自定义的错误页面

当我
throw new ZSException();的时候,竟然无法捕获到,调试的时候根本进不到Application_OnError
如果
throw new Exception();
实在是很纳闷,有没有人遇到过同样的问题呢?
...全文
301 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
way106vip 2009-03-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 greatbag 的回复:]
如果
throw new Exception(); 就能捕获到
[/Quote]
就是这个方法
zzxap 2009-03-06
  • 打赏
  • 举报
回复
Catch Exception ex

response.write(ex.Message.ToString())
dxpws 2009-02-26
  • 打赏
  • 举报
回复
good luck
大神来了丶 2009-02-23
  • 打赏
  • 举报
回复
帮顶
greatbag 2009-02-22
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 lextm 的回复:]
我的意思是在你的Web Application工程或者Web Site中的global.asax中处理这个异常。至于你试过之后还是捕捉不到,那就十分值得怀疑你的整个代码逻辑到底是怎么做的,就不是只看几行代码就能给出解答的了。

至于CommunityServer,光看代码来学习总是会有这样或者那样的问题,因为你根本不知道人家为什么会这样设计。还是看看基础的书籍和CodeProject带讲解的代码更适合一些。
[/Quote]
这是异常类的代码,如果你有空的话帮我看看是不是哪里出了错

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;

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 { }
}
}
}

lextm 2009-02-22
  • 打赏
  • 举报
回复
我的意思是在你的Web Application工程或者Web Site中的global.asax中处理这个异常。至于你试过之后还是捕捉不到,那就十分值得怀疑你的整个代码逻辑到底是怎么做的,就不是只看几行代码就能给出解答的了。

至于CommunityServer,光看代码来学习总是会有这样或者那样的问题,因为你根本不知道人家为什么会这样设计。还是看看基础的书籍和CodeProject带讲解的代码更适合一些。
jedliu 2009-02-21
  • 打赏
  • 举报
回复
将所有的东西都通过Exception来捕捉不是好的实现方式,具体情况具体实现,你可以用这个试试,System.Web.HttpApplication.Error
greatbag 2009-02-21
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 lextm 的回复:]
原因说穿了就很简单。

HTTP module和普通web应用在服务器中运行时,处于一条流水线的不同部位。

你在web应用的global.asax里面抓到这个自定义异常,但是随着请求在流水线中移动,ASP.NET运行时会根据它生成一个普通的HttpException对象,仅仅保留异常信息之类简单信息,HTTP module就会看到这个新的异常对象。

因此,对于自定义异常的情况,最好还是在web应用里面做处理,而不要等到HTTP module参与的时候。
[/Quote]
你的意思是说用global.asax捕捉?即使用global.asax捕捉,也一样无法捕捉到..我已经试过

我写的这个捕获异常是依照CommunityServer 写的,按理它那个是能正常动作,但是我不知道为什么写的就捕捉不到
greatbag 2009-02-21
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 jedliu 的回复:]
将所有的东西都通过Exception来捕捉不是好的实现方式,具体情况具体实现,你可以用这个试试,System.Web.HttpApplication.Error
[/Quote]
我不是很明白你的意思
void IHttpModule.Init(HttpApplication context)
{
context.Error += new EventHandler(this.Application_OnError);
}
这里用的就是你说的System.Web.HttpApplication.Error
greatbag 2009-02-21
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 feifeiyiwen 的回复:]
应该可以的呀
[/Quote]
是应该可以的,但是它就是不出来
feifeiyiwen 2009-02-21
  • 打赏
  • 举报
回复
应该可以的呀
lextm 2009-02-21
  • 打赏
  • 举报
回复
原因说穿了就很简单。

HTTP module和普通web应用在服务器中运行时,处于一条流水线的不同部位。

你在web应用的global.asax里面抓到这个自定义异常,但是随着请求在流水线中移动,ASP.NET运行时会根据它生成一个普通的HttpException对象,仅仅保留异常信息之类简单信息,HTTP module就会看到这个新的异常对象。

因此,对于自定义异常的情况,最好还是在web应用里面做处理,而不要等到HTTP module参与的时候。
greatbag 2009-02-19
  • 打赏
  • 举报
回复
但是依然还是捕获不到异常,真的是很诡异的问题
greatbag 2009-02-19
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 fanliang11 的回复:]

你没初始化BASE。

[/Quote]
已经有初始化了,谢谢


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();
}
fanliang11 2009-02-19
  • 打赏
  • 举报
回复

public class ZSException : ApplicationException
{
public ZSException () {}

public ZSException (string message) : base(message) {}

public ZSException (string message, Exception e) : base(message, e) {}
}


你没初始化BASE。

你标题是这样写的,ihttpmodule 无法 捕获 自定义异常 ,OK?

greatbag 2009-02-19
  • 打赏
  • 举报
回复
异常是继承于ApplicationException 或Exception
HTTPMODULE才是继承于IHttpModule,
这是两个不同的类,不同的概念
fanliang11 2009-02-19
  • 打赏
  • 举报
回复

public class ZSException : IHttpModule



我记得好像是继承IHttpModule,不是ApplicationException
greatbag 2009-02-19
  • 打赏
  • 举报
回复
ZSException 继承自 ApplicationException

try
{
}
catch(ZSException zex)
{
throw zex;
}
得到
greatbag 2009-02-19
  • 打赏
  • 举报
回复
try
{
//错误
}
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
{
....//重定向到自定义的错误页面



这样就不行了
sxmonsy 2009-02-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 greatbag 的回复:]
ZSException 继承自 ApplicationException

try
{
}
catch(ZSException zex)
{
throw zex;
}
得到
[/Quote]
这样得不到?
加载更多回复(7)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧