如果 
throw new Exception(); 就能捕获到

解决方案 »

  1.   

    zsException、ZSException和public class ZSException分清楚,你的命名方式让人看着迷糊。
      

  2.   

    你的ZSException侍从Exception里继承的吗?
      

  3.   

    try 
    {
    }
    catch(ZSException zex)
    {
     throw zex; 
    }
    看看
      

  4.   

    ZSException 继承自 ApplicationException try 


    catch(ZSException zex) 

    throw zex; 

    得到
      

  5.   


    public class ZSException : IHttpModule我记得好像是继承IHttpModule,不是ApplicationException 
      

  6.   

    异常是继承于ApplicationException 或Exception 
    HTTPMODULE才是继承于IHttpModule,
    这是两个不同的类,不同的概念
      

  7.   


    public class ZSException : ApplicationException 
    {
      public ZSException () {}   public ZSException (string message) : base(message) {}   public ZSException (string message, Exception e) : base(message, e) {}
    }
    你没初始化BASE。你标题是这样写的,ihttpmodule 无法 捕获 自定义异常  ,OK?
      

  8.   

    已经有初始化了,谢谢
    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();
            }
      

  9.   

    为什么我可以呢?try
    {
       //错误
    }
    catch (Exception e)
    {
           throw new ZSException("未初始化.", e);
    }
      

  10.   

    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 
                { 
    ....//重定向到自定义的错误页面 这样就不行了
      

  11.   

    将所有的东西都通过Exception来捕捉不是好的实现方式,具体情况具体实现,你可以用这个试试,System.Web.HttpApplication.Error
      

  12.   

    原因说穿了就很简单。HTTP module和普通web应用在服务器中运行时,处于一条流水线的不同部位。你在web应用的global.asax里面抓到这个自定义异常,但是随着请求在流水线中移动,ASP.NET运行时会根据它生成一个普通的HttpException对象,仅仅保留异常信息之类简单信息,HTTP module就会看到这个新的异常对象。因此,对于自定义异常的情况,最好还是在web应用里面做处理,而不要等到HTTP module参与的时候。
      

  13.   

    我不是很明白你的意思 
    void IHttpModule.Init(HttpApplication context) 
            {            
                context.Error += new EventHandler(this.Application_OnError); 
            } 
    这里用的就是你说的System.Web.HttpApplication.Error
      

  14.   

    你的意思是说用global.asax捕捉?即使用global.asax捕捉,也一样无法捕捉到..我已经试过我写的这个捕获异常是依照CommunityServer 写的,按理它那个是能正常动作,但是我不知道为什么写的就捕捉不到
      

  15.   

    我的意思是在你的Web Application工程或者Web Site中的global.asax中处理这个异常。至于你试过之后还是捕捉不到,那就十分值得怀疑你的整个代码逻辑到底是怎么做的,就不是只看几行代码就能给出解答的了。至于CommunityServer,光看代码来学习总是会有这样或者那样的问题,因为你根本不知道人家为什么会这样设计。还是看看基础的书籍和CodeProject带讲解的代码更适合一些。
      

  16.   

    这是异常类的代码,如果你有空的话帮我看看是不是哪里出了错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 { }
            }
        }
    }
      

  17.   

     Catch   Exception ex
                
        response.write(ex.Message.ToString())