Beispiel #1
0
        /// <summary>
        /// Checks if there is a policy entry that matches
        /// the type of the exception object specified by the
        /// <see cref="Exception"/> parameter
        /// and if so, invokes the handlers associated with that entry.
        /// </summary>
        /// <param name="ex">The <c>Exception</c> to handle.</param>
        /// <returns>Whether or not a rethrow is recommended.</returns>
        /// <remarks>
        /// The algorithm for matching the exception object to a
        /// set of handlers mimics that of a standard .NET exception policy.
        /// The specified exception object will be matched to a single
        /// exception policy entry by traversing its inheritance hierarchy.
        /// This means that if a <c>FileNotFoundException</c>, for example, is
        /// caught, but the only exception type that the exception policy
        /// knows how to handle is System.Exception, the event handlers
        /// for <c>System.Exception</c> will be invoked because
        /// <c>FileNotFoundException</c> ultimately derives from <c>System.Exception</c>.
        /// </remarks>
        private bool HandleException(Exception ex)
        {
            Type exceptionType         = ex.GetType();
            ExceptionPolicyEntry entry = this.FindExceptionPolicyEntry(exceptionType);

            bool recommendRethrow = false;

            if (entry == null)
            {
                // If there is no Entry for this type / policy than we recommend a rethrow.
                recommendRethrow = true;
                ExceptionNotHandledEvent.Fire();
            }
            else
            {
                try
                {
                    recommendRethrow = entry.Handle(ex);
                    ExceptionHandledEvent.Fire();
                }
                catch (ExceptionHandlingException)
                {
                    ExceptionNotHandledEvent.Fire();
                    throw;
                }
            }
            return(recommendRethrow);
        }
        public bool HandleException(Exception exceptionToHandle)
        {
            if (exceptionToHandle == null)
            {
                throw new ArgumentNullException("exceptionToHandler");
            }
            ExceptionPolicyEntry entry = GetPolicyEntry(exceptionToHandle);

            if (entry == null)
            {
                return(true);
            }
            return(entry.Handle(exceptionToHandle));
        }