Example #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);
        }
Example #2
0
 private Type GetExceptionType(ExceptionTypeData typeData, string policyName)
 {
     try
     {
         return(Type.GetType(typeData.TypeName, true));
     }
     catch (TypeLoadException e)
     {
         ExceptionUtility.LogHandlingException(policyName, null, null, e);
         ExceptionNotHandledEvent.Fire();
         throw new ExceptionHandlingException(SR.ExceptionUnknownExceptionTypeInConfiguration(typeData.TypeName), e);
     }
 }
Example #3
0
 private static ExceptionPolicy GetExceptionPolicy(Exception exception, string policyName, ConfigurationContext configurationContext)
 {
     try
     {
         ExceptionPolicyFactory factory = new ExceptionPolicyFactory(configurationContext);
         return(factory.CreateExceptionPolicy(policyName, exception));
     }
     catch (ConfigurationException ex)
     {
         ExceptionUtility.LogHandlingException(policyName, ex, null, exception);
         ExceptionNotHandledEvent.Fire();
         throw new ExceptionHandlingException(ex.Message, ex);
     }
     catch (InvalidOperationException ex)
     {
         ExceptionUtility.LogHandlingException(policyName, ex, null, exception);
         ExceptionNotHandledEvent.Fire();
         throw new ExceptionHandlingException(ex.Message, ex);
     }
 }
 private void FireExceptionNotHandledEvent()
 {
     ExceptionNotHandledEvent.Fire();
 }