/* Exceptional.cs Copyright © 2008 Diversified Technical Systems, Inc. All Rights Reserved */ using System; using System.Runtime.Serialization; using DTS.Common.Utilities.DotNetProgrammingConstructs; using DTS.Common.Utilities.Properties; namespace DTS.Common.Utilities { /// /// "Ultimate" base class for all DTS classes that expect to throw exceptions. /// Deriving classes from this class allows exceptions to be trapped based on /// the class type that threw them. /// /// /// /// Sample usage: /// public class A : Exceptional /// { /// public void ScrewItUp( ) /// { /// private bool error = true; /// if ( error ) throw new A.Exception( "Class A-specific screwup." ); /// } /// } /// /// ... /// /// try /// { /// A.ScrewItUp( ); /// B.ScrewItUp( ); /// C.ScrewItUp( ); /// } /// catch ( A.Exception ex ) /// { /// // Can pick A's exceptions out of a crowd, or not and just treat it /// // polymorphically as a System.Exception. /// } /// /// [Serializable] public abstract class Exceptional { /// /// Initialize an instance of the DerivedClass.Exception class. /// public class Exception : ApplicationException { /// /// create a object /// public Exception() { } /// /// create a object /// /// a message that describes the error public Exception(string msg) : base(msg) { } /// /// create a object /// /// a message that describes the error /// The exception that is the cause of the current exception. public Exception(string msg, System.Exception innerEx) : base(msg, innerEx) { } protected Exception( SerializationInfo info, StreamingContext context) : base(info, context) { } } /// /// Initialize an instance of the DerivedClass.UserException class. /// public class UserException : Exception { /// /// create a user Exception with a description of the event /// /// description of error public UserException(string msg) : base(msg) { } } /// /// Walk the nested exception tree and generate a single string from their messages. /// /// /// /// The parent . /// /// /// /// The concatenation of the exception tree's messages. /// /// public static string GenerateLogMessageFromExceptionTree(System.Exception ex) { return GenerateMessageFromExceptionTree(ex, true, true, true); } public static string GenerateUIMessageFromExceptionTree(System.Exception ex) { return GenerateMessageFromExceptionTree(ex, true, true, false); } /// /// Walk the nested exception tree and generate a single string from their messages. /// /// /// /// The parent . /// /// /// /// true to insert a newline between each exception's message, /// false to insert a delimiter between each instead. /// /// /// /// The concatenation of the exception tree's messages. /// /// public static string GenerateLogMessageFromExceptionTree(System.Exception ex, bool addNewlines) { return GenerateMessageFromExceptionTree(ex, addNewlines, true, true); } public static string GenerateUIMessageFromExceptionTree(System.Exception ex, bool addNewLines) { return GenerateMessageFromExceptionTree(ex, addNewLines, true, false); } /// /// Legacy wrapper for walking the nested exception tree and generating a single string from their messages /// /// /// /// The parent . /// /// /// /// The concatenation of the exception tree's messages. /// public static string GenerateMessageFromExceptionTree(System.Exception ex) { return GenerateMessageFromExceptionTree(ex, true, true, true); } /// /// Walk the nested exception tree and generate a single string from their messages. /// /// /// /// The parent . /// /// /// /// true to insert a newline between each exception's message, /// false to insert a delimiter between each instead. /// /// /// /// true to append "because" after each exception message in /// string and end deepest message with a period, false to leave messages raw. /// /// /// /// /// The concatenation of the exception tree's messages. /// /// public static string GenerateMessageFromExceptionTree(System.Exception ex, bool addNewlines, bool makeSentence, bool bLogMessage) { var message = ""; var stackTrace = ""; var currentEx = ex; while (null != currentEx) { if (bLogMessage) { message += string.Format(Resources.Exceptional_GenerateMessageFromExceptionTree_MessageFormatString, currentEx.Message, makeSentence && null != currentEx.InnerException ? Resources.Exceptional_GenerateMessageFromExceptionTree_BecauseString : Resources.Exceptional_GenerateMessageFromExceptionTree_ErrorTextTerminatorString, null != currentEx.InnerException ? (addNewlines ? "\n" : " : ") : ""); } else { message = currentEx.Message; } stackTrace += currentEx.StackTrace; currentEx = currentEx.InnerException; } #if ( DEBUG ) if (bLogMessage) { message += "\n\n*** Debug Mode Stack Trace ***\n" + stackTrace; } #endif return message.EndsWith( string.Format(Resources.Exceptional_GenerateMessageFromExceptionTree_EndsWithFormatString, Resources.Exceptional_GenerateMessageFromExceptionTree_ErrorTextTerminatorString, Resources.Exceptional_GenerateMessageFromExceptionTree_ErrorTextTerminatorString)) ? message.Remove(message.LastIndexOf(Resources.Exceptional_GenerateMessageFromExceptionTree_ErrorTextTerminatorString, StringComparison.Ordinal)) : message; } /// /// Extract the first exception of the specified class type from the specified /// exception's exception tree. /// /// /// /// The type of System.Exception-derived class to be extracted. /// /// /// /// The -derived class to have its exception tree /// searched. /// /// /// /// The first nested innner exception of type ExceptionType; null otherwise. /// /// public static TExceptionType ExtractFirstExceptionOfTypeFromExceptionTree(System.Exception ex) where TExceptionType : System.Exception { var currentEx = ex; while (null != currentEx) { if (currentEx is TExceptionType exceptionType) return exceptionType; currentEx = currentEx.InnerException; } return null; } /// /// Determine whether or not the specified exception's exception tree contains /// a UserException-derived exception. /// /// /// /// The -derived class to have its exception tree /// searched. /// /// /// /// true if the specified exception tree contains a /// UserException-type exception; false otherwise. /// /// public static bool ExceptionTreeContainsUserException(System.Exception ex) { return null != ExtractFirstExceptionOfTypeFromExceptionTree(ex); } } }