Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / BCL / System / AggregateException.cs / 1305376 / AggregateException.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ // // AggregateException.cs // //[....] // // Public type to communicate multiple failures to an end-user. // // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.Globalization; using System.Runtime.Serialization; using System.Security; using System.Threading; namespace System { ///Represents one or more errors that occur during application execution. ////// [Serializable] [DebuggerDisplay("Count = {InnerExceptions.Count}")] public class AggregateException : Exception { private ReadOnlyCollectionis used to consolidate multiple failures into a single, throwable /// exception object. /// m_innerExceptions; // Complete set of exceptions. /// /// Initializes a new instance of the public AggregateException() : base(Environment.GetResourceString("AggregateException_ctor_DefaultMessage")) { m_innerExceptions = new ReadOnlyCollectionclass. /// (new Exception[0]); } /// /// Initializes a new instance of the /// The error message that explains the reason for the exception. public AggregateException(string message) : base(message) { m_innerExceptions = new ReadOnlyCollectionclass with /// a specified error message. /// (new Exception[0]); } /// /// Initializes a new instance of the /// The error message that explains the reason for the exception. /// The exception that is the cause of the current exception. ///class with a specified error /// message and a reference to the inner exception that is the cause of this exception. /// The public AggregateException(string message, Exception innerException) : base(message, innerException) { if (innerException == null) { throw new ArgumentNullException("innerException"); } m_innerExceptions = new ReadOnlyCollectionargument /// is null. (new Exception[] { innerException }); } /// /// Initializes a new instance of the /// The exceptions that are the cause of the current exception. ///class with /// references to the inner exceptions that are the cause of this exception. /// The ///argument /// is null. An element of public AggregateException(IEnumerableis /// null. innerExceptions) : this(Environment.GetResourceString("AggregateException_ctor_DefaultMessage"), innerExceptions) { } /// /// Initializes a new instance of the /// The exceptions that are the cause of the current exception. ///class with /// references to the inner exceptions that are the cause of this exception. /// The ///argument /// is null. An element of public AggregateException(params Exception[] innerExceptions) : this(Environment.GetResourceString("AggregateException_ctor_DefaultMessage"), innerExceptions) { } ///is /// null. /// Initializes a new instance of the /// The error message that explains the reason for the exception. /// The exceptions that are the cause of the current exception. ///class with a specified error /// message and references to the inner exceptions that are the cause of this exception. /// The ///argument /// is null. An element of public AggregateException(string message, IEnumerableis /// null. innerExceptions) : this(message, innerExceptions == null ? (List )null : new List (innerExceptions)) { } /// /// Initializes a new instance of the /// The error message that explains the reason for the exception. /// The exceptions that are the cause of the current exception. ///class with a specified error /// message and references to the inner exceptions that are the cause of this exception. /// The ///argument /// is null. An element of public AggregateException(string message, params Exception[] innerExceptions) : this(message, (IListis /// null. )innerExceptions) { } /// /// Allocates a new aggregate exception with the specified message and list of inner exceptions. /// /// The error message that explains the reason for the exception. /// The exceptions that are the cause of the current exception. ///The ///argument /// is null. An element of private AggregateException(string message, IListis /// null. innerExceptions) : base(message, innerExceptions != null && innerExceptions.Count > 0 ? innerExceptions[0] : null) { if (innerExceptions == null) { throw new ArgumentNullException("innerExceptions"); } // Copy exceptions to our internal array and validate them. We must copy them, // because we're going to put them into a ReadOnlyCollection which simply reuses // the list passed in to it. We don't want callers subsequently mutating. Exception[] exceptionsCopy = new Exception[innerExceptions.Count]; for (int i = 0; i < exceptionsCopy.Length; i++) { exceptionsCopy[i] = innerExceptions[i]; if (exceptionsCopy[i] == null) { throw new ArgumentException(Environment.GetResourceString("AggregateException_ctor_InnerExceptionNull")); } } m_innerExceptions = new ReadOnlyCollection (exceptionsCopy); } /// /// Initializes a new instance of the /// Theclass with serialized data. /// that holds /// the serialized object data about the exception being thrown. /// The that /// contains contextual information about the source or destination. /// The ///argument is null. The exception could not be deserialized correctly. [SecurityCritical] protected AggregateException(SerializationInfo info, StreamingContext context) : base(info, context) { if (info == null) { throw new ArgumentNullException("info"); } Exception[] innerExceptions = info.GetValue("InnerExceptions", typeof(Exception[])) as Exception[]; if (innerExceptions == null) { throw new SerializationException(Environment.GetResourceString("AggregateException_DeserializationFailure")); } m_innerExceptions = new ReadOnlyCollection(innerExceptions); } /// /// Sets the /// Thewith information about /// the exception. /// that holds /// the serialized object data about the exception being thrown. /// The that /// contains contextual information about the source or destination. /// The [SecurityCritical] public override void GetObjectData(SerializationInfo info, StreamingContext context) { if (info == null) { throw new ArgumentNullException("info"); } base.GetObjectData(info, context); Exception[] innerExceptions = new Exception[m_innerExceptions.Count]; m_innerExceptions.CopyTo(innerExceptions, 0); info.AddValue("InnerExceptions", innerExceptions, typeof(Exception[])); } ///argument is null. /// Returns the public override Exception GetBaseException() { // Returns the first inner AggregateException that contains more or less than one inner exception // Recursively traverse the inner exceptions as long as the inner exception of type AggregateException and has only one inner exception Exception back = this; AggregateException backAsAggregate = this; while (backAsAggregate != null && backAsAggregate.InnerExceptions.Count == 1) { back = back.InnerException; backAsAggregate = back as AggregateException; } return back; } ///that is the root cause of this exception. /// /// Gets a read-only collection of the public ReadOnlyCollectioninstances that caused the /// current exception. /// InnerExceptions { get { return m_innerExceptions; } } #if !FEATURE_CORECLR /// /// Invokes a handler on each /// The predicate to execute for each exception. The predicate accepts as an /// argument thecontained by this . /// to be processed and returns a Boolean to indicate /// whether the exception was handled. /// /// Each invocation of the ///returns true or false to indicate whether the /// was handled. After all invocations, if any exceptions went /// unhandled, all unhandled exceptions will be put into a new /// which will be thrown. Otherwise, the method simply returns. If any /// invocations of the throws an exception, it will halt the processing /// of any more exceptions and immediately propagate the thrown exception as-is. /// An exception contained by this ///was not handled. The public void Handle(Funcargument is /// null. predicate) { if (predicate == null) { throw new ArgumentNullException("predicate"); } List unhandledExceptions = null; for (int i = 0; i < m_innerExceptions.Count; i++) { // If the exception was not handled, lazily allocate a list of unhandled // exceptions (to be rethrown later) and add it. if (!predicate(m_innerExceptions[i])) { if (unhandledExceptions == null) { unhandledExceptions = new List (); } unhandledExceptions.Add(m_innerExceptions[i]); } } // If there are unhandled exceptions remaining, throw them. if (unhandledExceptions != null) { throw new AggregateException(Message, unhandledExceptions); } } #endif //!FEATURE_CORECLR /// /// Flattens an ///instances into a single, new instance. /// A new, flattened ///. /// If any inner exceptions are themselves instances of /// public AggregateException Flatten() { // Initialize a collection to contain the flattened exceptions. List, this method will recursively flatten all of them. The /// inner exceptions returned in the new /// will be the union of all of the the inner exceptions from exception tree rooted at the provided /// instance. /// flattenedExceptions = new List (); // Create a list to remember all aggregates to be flattened, this will be accessed like a FIFO queue List exceptionsToFlatten = new List (); exceptionsToFlatten.Add(this); int nDequeueIndex = 0; // Continue removing and recursively flattening exceptions, until there are no more. while (exceptionsToFlatten.Count > nDequeueIndex) { // dequeue one from exceptionsToFlatten IList currentInnerExceptions = exceptionsToFlatten[nDequeueIndex++].InnerExceptions; for (int i = 0; i < currentInnerExceptions.Count; i++) { Exception currentInnerException = currentInnerExceptions[i]; if (currentInnerException == null) { continue; } AggregateException currentInnerAsAggregate = currentInnerException as AggregateException; // If this exception is an aggregate, keep it around for later. Otherwise, // simply add it to the list of flattened exceptions to be returned. if (currentInnerAsAggregate != null) { exceptionsToFlatten.Add(currentInnerAsAggregate); } else { flattenedExceptions.Add(currentInnerException); } } } return new AggregateException(Message, flattenedExceptions); } /// /// Creates and returns a string representation of the current ///. /// A string representation of the current exception. public override string ToString() { string text = base.ToString(); for (int i = 0; i < m_innerExceptions.Count; i++) { text = String.Format( CultureInfo.InvariantCulture, Environment.GetResourceString("AggregateException_ToString"), text, Environment.NewLine, i, m_innerExceptions[i].ToString(), "<---", Environment.NewLine); } return text; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ // // AggregateException.cs // //[....] // // Public type to communicate multiple failures to an end-user. // // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.Globalization; using System.Runtime.Serialization; using System.Security; using System.Threading; namespace System { ///Represents one or more errors that occur during application execution. ////// [Serializable] [DebuggerDisplay("Count = {InnerExceptions.Count}")] public class AggregateException : Exception { private ReadOnlyCollectionis used to consolidate multiple failures into a single, throwable /// exception object. /// m_innerExceptions; // Complete set of exceptions. /// /// Initializes a new instance of the public AggregateException() : base(Environment.GetResourceString("AggregateException_ctor_DefaultMessage")) { m_innerExceptions = new ReadOnlyCollectionclass. /// (new Exception[0]); } /// /// Initializes a new instance of the /// The error message that explains the reason for the exception. public AggregateException(string message) : base(message) { m_innerExceptions = new ReadOnlyCollectionclass with /// a specified error message. /// (new Exception[0]); } /// /// Initializes a new instance of the /// The error message that explains the reason for the exception. /// The exception that is the cause of the current exception. ///class with a specified error /// message and a reference to the inner exception that is the cause of this exception. /// The public AggregateException(string message, Exception innerException) : base(message, innerException) { if (innerException == null) { throw new ArgumentNullException("innerException"); } m_innerExceptions = new ReadOnlyCollectionargument /// is null. (new Exception[] { innerException }); } /// /// Initializes a new instance of the /// The exceptions that are the cause of the current exception. ///class with /// references to the inner exceptions that are the cause of this exception. /// The ///argument /// is null. An element of public AggregateException(IEnumerableis /// null. innerExceptions) : this(Environment.GetResourceString("AggregateException_ctor_DefaultMessage"), innerExceptions) { } /// /// Initializes a new instance of the /// The exceptions that are the cause of the current exception. ///class with /// references to the inner exceptions that are the cause of this exception. /// The ///argument /// is null. An element of public AggregateException(params Exception[] innerExceptions) : this(Environment.GetResourceString("AggregateException_ctor_DefaultMessage"), innerExceptions) { } ///is /// null. /// Initializes a new instance of the /// The error message that explains the reason for the exception. /// The exceptions that are the cause of the current exception. ///class with a specified error /// message and references to the inner exceptions that are the cause of this exception. /// The ///argument /// is null. An element of public AggregateException(string message, IEnumerableis /// null. innerExceptions) : this(message, innerExceptions == null ? (List )null : new List (innerExceptions)) { } /// /// Initializes a new instance of the /// The error message that explains the reason for the exception. /// The exceptions that are the cause of the current exception. ///class with a specified error /// message and references to the inner exceptions that are the cause of this exception. /// The ///argument /// is null. An element of public AggregateException(string message, params Exception[] innerExceptions) : this(message, (IListis /// null. )innerExceptions) { } /// /// Allocates a new aggregate exception with the specified message and list of inner exceptions. /// /// The error message that explains the reason for the exception. /// The exceptions that are the cause of the current exception. ///The ///argument /// is null. An element of private AggregateException(string message, IListis /// null. innerExceptions) : base(message, innerExceptions != null && innerExceptions.Count > 0 ? innerExceptions[0] : null) { if (innerExceptions == null) { throw new ArgumentNullException("innerExceptions"); } // Copy exceptions to our internal array and validate them. We must copy them, // because we're going to put them into a ReadOnlyCollection which simply reuses // the list passed in to it. We don't want callers subsequently mutating. Exception[] exceptionsCopy = new Exception[innerExceptions.Count]; for (int i = 0; i < exceptionsCopy.Length; i++) { exceptionsCopy[i] = innerExceptions[i]; if (exceptionsCopy[i] == null) { throw new ArgumentException(Environment.GetResourceString("AggregateException_ctor_InnerExceptionNull")); } } m_innerExceptions = new ReadOnlyCollection (exceptionsCopy); } /// /// Initializes a new instance of the /// Theclass with serialized data. /// that holds /// the serialized object data about the exception being thrown. /// The that /// contains contextual information about the source or destination. /// The ///argument is null. The exception could not be deserialized correctly. [SecurityCritical] protected AggregateException(SerializationInfo info, StreamingContext context) : base(info, context) { if (info == null) { throw new ArgumentNullException("info"); } Exception[] innerExceptions = info.GetValue("InnerExceptions", typeof(Exception[])) as Exception[]; if (innerExceptions == null) { throw new SerializationException(Environment.GetResourceString("AggregateException_DeserializationFailure")); } m_innerExceptions = new ReadOnlyCollection(innerExceptions); } /// /// Sets the /// Thewith information about /// the exception. /// that holds /// the serialized object data about the exception being thrown. /// The that /// contains contextual information about the source or destination. /// The [SecurityCritical] public override void GetObjectData(SerializationInfo info, StreamingContext context) { if (info == null) { throw new ArgumentNullException("info"); } base.GetObjectData(info, context); Exception[] innerExceptions = new Exception[m_innerExceptions.Count]; m_innerExceptions.CopyTo(innerExceptions, 0); info.AddValue("InnerExceptions", innerExceptions, typeof(Exception[])); } ///argument is null. /// Returns the public override Exception GetBaseException() { // Returns the first inner AggregateException that contains more or less than one inner exception // Recursively traverse the inner exceptions as long as the inner exception of type AggregateException and has only one inner exception Exception back = this; AggregateException backAsAggregate = this; while (backAsAggregate != null && backAsAggregate.InnerExceptions.Count == 1) { back = back.InnerException; backAsAggregate = back as AggregateException; } return back; } ///that is the root cause of this exception. /// /// Gets a read-only collection of the public ReadOnlyCollectioninstances that caused the /// current exception. /// InnerExceptions { get { return m_innerExceptions; } } #if !FEATURE_CORECLR /// /// Invokes a handler on each /// The predicate to execute for each exception. The predicate accepts as an /// argument thecontained by this . /// to be processed and returns a Boolean to indicate /// whether the exception was handled. /// /// Each invocation of the ///returns true or false to indicate whether the /// was handled. After all invocations, if any exceptions went /// unhandled, all unhandled exceptions will be put into a new /// which will be thrown. Otherwise, the method simply returns. If any /// invocations of the throws an exception, it will halt the processing /// of any more exceptions and immediately propagate the thrown exception as-is. /// An exception contained by this ///was not handled. The public void Handle(Funcargument is /// null. predicate) { if (predicate == null) { throw new ArgumentNullException("predicate"); } List unhandledExceptions = null; for (int i = 0; i < m_innerExceptions.Count; i++) { // If the exception was not handled, lazily allocate a list of unhandled // exceptions (to be rethrown later) and add it. if (!predicate(m_innerExceptions[i])) { if (unhandledExceptions == null) { unhandledExceptions = new List (); } unhandledExceptions.Add(m_innerExceptions[i]); } } // If there are unhandled exceptions remaining, throw them. if (unhandledExceptions != null) { throw new AggregateException(Message, unhandledExceptions); } } #endif //!FEATURE_CORECLR /// /// Flattens an ///instances into a single, new instance. /// A new, flattened ///. /// If any inner exceptions are themselves instances of /// public AggregateException Flatten() { // Initialize a collection to contain the flattened exceptions. List, this method will recursively flatten all of them. The /// inner exceptions returned in the new /// will be the union of all of the the inner exceptions from exception tree rooted at the provided /// instance. /// flattenedExceptions = new List (); // Create a list to remember all aggregates to be flattened, this will be accessed like a FIFO queue List exceptionsToFlatten = new List (); exceptionsToFlatten.Add(this); int nDequeueIndex = 0; // Continue removing and recursively flattening exceptions, until there are no more. while (exceptionsToFlatten.Count > nDequeueIndex) { // dequeue one from exceptionsToFlatten IList currentInnerExceptions = exceptionsToFlatten[nDequeueIndex++].InnerExceptions; for (int i = 0; i < currentInnerExceptions.Count; i++) { Exception currentInnerException = currentInnerExceptions[i]; if (currentInnerException == null) { continue; } AggregateException currentInnerAsAggregate = currentInnerException as AggregateException; // If this exception is an aggregate, keep it around for later. Otherwise, // simply add it to the list of flattened exceptions to be returned. if (currentInnerAsAggregate != null) { exceptionsToFlatten.Add(currentInnerAsAggregate); } else { flattenedExceptions.Add(currentInnerException); } } } return new AggregateException(Message, flattenedExceptions); } /// /// Creates and returns a string representation of the current ///. /// A string representation of the current exception. public override string ToString() { string text = base.ToString(); for (int i = 0; i < m_innerExceptions.Count; i++) { text = String.Format( CultureInfo.InvariantCulture, Environment.GetResourceString("AggregateException_ToString"), text, Environment.NewLine, i, m_innerExceptions[i].ToString(), "<---", Environment.NewLine); } return text; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SerializationInfo.cs
- AutomationFocusChangedEventArgs.cs
- Validator.cs
- CreateUserWizard.cs
- ListChangedEventArgs.cs
- Utilities.cs
- TreeChangeInfo.cs
- StructuredProperty.cs
- ClickablePoint.cs
- RuntimeArgumentHandle.cs
- ConnectionsZoneDesigner.cs
- StrokeCollection.cs
- SpotLight.cs
- XDRSchema.cs
- CapiHashAlgorithm.cs
- BindingNavigator.cs
- TextFormatter.cs
- InstanceCreationEditor.cs
- XmlWrappingReader.cs
- UIAgentAsyncBeginRequest.cs
- WmlSelectionListAdapter.cs
- CapabilitiesUse.cs
- MethodCallTranslator.cs
- RayHitTestParameters.cs
- PrivacyNoticeBindingElement.cs
- RegionInfo.cs
- QilTargetType.cs
- BaseDataList.cs
- DragStartedEventArgs.cs
- CodeTypeDelegate.cs
- EventProviderBase.cs
- ColumnWidthChangingEvent.cs
- SystemIPAddressInformation.cs
- FontStyle.cs
- safemediahandle.cs
- CompilationUnit.cs
- Task.cs
- XmlReflectionMember.cs
- EventWaitHandleSecurity.cs
- _FtpDataStream.cs
- TimeSpanStorage.cs
- RegexRunner.cs
- SqlServer2KCompatibilityAnnotation.cs
- ConstrainedDataObject.cs
- BasicCellRelation.cs
- Component.cs
- TextProviderWrapper.cs
- ComboBoxRenderer.cs
- BamlLocalizationDictionary.cs
- DataBindingCollection.cs
- CompiledAction.cs
- SqlXmlStorage.cs
- cookiecontainer.cs
- SqlNodeTypeOperators.cs
- HtmlSelect.cs
- util.cs
- ProxyWebPart.cs
- AppDomain.cs
- SoapIgnoreAttribute.cs
- StreamSecurityUpgradeAcceptor.cs
- ellipse.cs
- ObjectHandle.cs
- LambdaCompiler.Expressions.cs
- MultiPageTextView.cs
- InfoCardMetadataExchangeClient.cs
- HtmlImage.cs
- Guid.cs
- PropertyConverter.cs
- QuaternionValueSerializer.cs
- HtmlTableCell.cs
- XmlIncludeAttribute.cs
- InfoCardRSAPKCS1SignatureFormatter.cs
- LocalValueEnumerator.cs
- DataSvcMapFileSerializer.cs
- _LoggingObject.cs
- ListSourceHelper.cs
- StrongNamePublicKeyBlob.cs
- ExpressionEditorAttribute.cs
- CustomLineCap.cs
- XmlEntity.cs
- EncoderFallback.cs
- TextTrailingWordEllipsis.cs
- MediaElement.cs
- InstancePersistenceCommandException.cs
- SupportingTokenListenerFactory.cs
- HtmlInputHidden.cs
- StatusBarItemAutomationPeer.cs
- IImplicitResourceProvider.cs
- ImagingCache.cs
- DBCommand.cs
- X509Extension.cs
- FileLogRecordStream.cs
- FillErrorEventArgs.cs
- TextTreeUndo.cs
- SqlUtil.cs
- UnsignedPublishLicense.cs
- EmptyEnumerator.cs
- OwnerDrawPropertyBag.cs
- ResourceDefaultValueAttribute.cs
- EntitySet.cs