/// <summary> /// 对此 <see cref="AggregateSourceException"/> 所包含的每个 <see cref="Exception"/> 调用处理程序。 /// </summary> /// <param name="predicate">要对每个异常执行的谓词。该谓词接受要处理的 <see cref="Exception"/> /// 作为参数,并返回指示异常是否已处理的布尔值。</param> /// <remarks> /// <paramref name="predicate"/> 的每个调用返回指示 <see cref="Exception"/> 是否已处理的 /// <c>true</c> 或 <c>false</c>。 /// 在对所有异常调用 <paramref name="predicate"/> 后,如果存在任何未处理的异常,它们将被放入新的 /// <see cref="AggregateSourceException"/> 中并引发异常。否则,只是返回 <see cref="Handle"/> 方法。 /// 如果 <paramref name="predicate"/> 的任何调用引发了异常,将暂停处理剩余的 <see cref="SourceException"/> /// 并立即传播新引发的异常。 /// </remarks> /// <exception cref="ArgumentNullException"><paramref name="predicate"/> 为 <c>null</c>。</exception> /// <exception cref="AggregateSourceException">此 <see cref="AggregateSourceException"/> /// 中包含任何未被处理的异常。</exception> public void Handle(Func <SourceException, bool> predicate) { if (predicate == null) { throw new ArgumentNullException("predicate"); } Contract.EndContractBlock(); List <Exception> unhandledExceptions = null; for (int i = 0; i < this.innerExps.Length; i++) { SourceException exp = this.innerExps[i]; if (predicate(exp)) { continue; } if (unhandledExceptions == null) { unhandledExceptions = new List <Exception>(); } unhandledExceptions.Add(exp); } if (unhandledExceptions != null) { throw new AggregateException(this.Message, unhandledExceptions); } }
/// <summary> /// 使用指定的错误消息和对导致此异常的内部异常的引用初始化 <see cref="AggregateSourceException"/> 类的新实例。 /// </summary> /// <param name="message">解释异常原因的错误消息。</param> /// <param name="innerException">导致当前异常的异常。</param> public AggregateSourceException(string message, SourceException innerException) : base(message, innerException) { if (innerException == null) { this.innerExps = ArrayExt.Empty <SourceException>(); this.innerExpsCollection = ReadOnlyCollection <SourceException> .Empty; } else { this.innerExps = new[] { innerException }; this.innerExpsCollection = new ReadOnlyCollection <SourceException>(innerExps); } }
/// <summary> /// 使用指定的错误消息和对导致此异常的内部异常的引用初始化 <see cref="AggregateSourceException"/> 类的新实例。 /// </summary> /// <param name="message">解释异常原因的错误消息。</param> /// <param name="innerException">导致当前异常的异常。</param> public AggregateSourceException(string message, Exception innerException) : base(message, innerException) { if (innerException == null) { this.innerExps = ArrayExt.Empty <SourceException>(); this.innerExpsCollection = ReadOnlyCollection <SourceException> .Empty; } else { SourceException sourceExp = innerException as SourceException; if (sourceExp == null) { throw CommonExceptions.InvalidCast(innerException.GetType(), typeof(SourceException)); } this.innerExps = new[] { sourceExp }; this.innerExpsCollection = new ReadOnlyCollection <SourceException>(innerExps); } }
/// <summary> /// 使用对导致此异常的内部异常的引用来初始化 <see cref="AggregateSourceException"/> 类的新实例。 /// </summary> /// <param name="innerExceptions">导致当前异常的异常。</param> public AggregateSourceException(SourceException[] innerExceptions) : this(Resources.GetString("DefaultAggregateExceptionMessage"), innerExceptions) { }
/// <summary> /// 使用指定的错误消息和对导致此异常的内部异常的引用初始化 <see cref="AggregateSourceException"/> 类的新实例。 /// </summary> /// <param name="message">解释异常原因的错误消息。</param> /// <param name="innerExceptions">导致当前异常的异常。</param> public AggregateSourceException(string message, SourceException[] innerExceptions) : base(message, innerExceptions != null && innerExceptions.Length > 0 ? innerExceptions[0] : null) { ExceptionHelper.CheckArgumentNull(innerExceptions, "innerExceptions"); this.innerExps = innerExceptions; for (int i = 0; i < innerExps.Length; i++) { if (innerExps[i] == null) { throw ExceptionHelper.InnerExceptionNull("innerExceptions"); } } readOnlyExps = new ReadOnlyCollection<SourceException>(innerExps); }