/// <summary>
        /// Handling exceptions.
        /// </summary>
        /// <param name="exception"></param>
        /// <param name="context"></param>
        /// <param name="deferred"></param>
        private void DoHandle(System.Exception exception, IRepeatContext context, ICollection <System.Exception> deferred)
        {
            // An exception alone is not sufficient grounds for not continuing
            System.Exception unwrappedException = UnwrapIfRethrown(exception);
            try
            {
                for (int i = _listeners.Length; i-- > 0;)
                {
                    IRepeatListener interceptor = _listeners[i];
                    // This is not an error - only log at debug level.
                    Logger.Debug(unwrappedException, "Exception intercepted ({0} of {1})", (i + 1), _listeners.Length);
                    interceptor.OnError(context, unwrappedException);
                }

                Logger.Debug("Handling exception: {0}, caused by: {1} : {2}",
                             exception.GetType().Name,
                             unwrappedException.GetType().Name,
                             unwrappedException.Message
                             );

                _exceptionHandler.HandleException(context, unwrappedException);
            }
            catch (System.Exception handled)
            {
                deferred.Add(handled);
            }
        }
        /// <summary>
        /// Registers given listener.
        /// </summary>
        /// <param name="listener"></param>
        public void RegisterListener(IRepeatListener listener)
        {
            List <IRepeatListener> list = new List <IRepeatListener>(_listeners)
            {
                listener
            };

            _listeners = list.ToArray();
        }
 /// <summary>
 ///  Convenience method to execute after interceptors on a callback result.
 /// </summary>
 /// <param name="context"></param>
 /// <param name="value"></param>
 protected void ExecuteAfterInterceptors(IRepeatContext context, RepeatStatus value)
 {
     // Don't re-throw exceptions here: let the exception handler deal with
     // that...
     if (value != null && value.IsContinuable())
     {
         for (int i = _listeners.Length; i-- > 0;)
         {
             IRepeatListener interceptor = _listeners[i];
             interceptor.After(context, value);
         }
     }
 }
 /// <summary>
 /// Registers array of listeners.
 /// </summary>
 /// <param name="listeners"></param>
 public void SetListeners(IRepeatListener[] listeners)
 {
     _listeners = listeners.ToArray();
 }
        /// <summary>
        /// Handling the finally from ExecuteInternal.
        /// </summary>
        /// <param name="deferred"></param>
        /// <param name="listeners"></param>
        /// <param name="context"></param>
        private void HandleFinally(ICollection<System.Exception> deferred,IRepeatListener[] listeners,IRepeatContext context)
        {
            try
            {
                if (deferred.Any())
                {
                    System.Exception exception = deferred.First();
                    Logger.Debug("Handling fatal exception explicitly (rethrowing first of {0}): {1} : {2}",
                        deferred.Count,
                        exception.GetType().Name,
                        exception.Message
                        );
                    Rethrow(exception);
                }
            }
            finally
            {
                try
                {
                    foreach (IRepeatListener interceptor in _listeners)
                    {
                        interceptor.Close(context);
                    }
                }
                finally
                {
                    context.Close();
                }

            }
        }
 /// <summary>
 /// Registers given listener.
 /// </summary>
 /// <param name="listener"></param>
 public void RegisterListener(IRepeatListener listener)
 {
     List<IRepeatListener> list = new List<IRepeatListener>(_listeners) { listener };
     _listeners = list.ToArray();
 }