using System; using System.Collections; using System.Diagnostics; using System.Text; using System.Collections.Generic; using System.Reflection; using System.IO; using System.Runtime.Serialization; namespace Sleis.Utility { /// /// Basic helper functions for dealing with exceptions. /// public static class ExceptionUtility { /// /// Returns a long string describing an exception, including information about /// any inner exceptions. /// public static string ToLongString(Exception inException) { return ToExceptionString(inException, true); } /// /// Returns a short string describing an exception, including information about /// any inner exceptions. /// public static string ToShortString(Exception inException) { return ToExceptionString(inException, false); } /// /// Returns the innermost exception's message. /// public static string GetDeepExceptionMessage(Exception inException) { Exception nextException = inException; while (nextException != null) { if (nextException.InnerException != null) { nextException = nextException.InnerException; } else { break; } } if (nextException == null) { return null; } return string.Format("Exception: {0}, Message: {1}", nextException.GetType().Name, nextException.Message); } public static string GetDeepExceptionMessageOnly(Exception inException) { Exception nextException = inException; while (nextException != null) { if (nextException.InnerException != null) { nextException = nextException.InnerException; } else { break; } } if (nextException == null) { return null; } return nextException.Message; } public static T ThrowIfNull(T parameter, string paramName) where T : class { if (parameter == null) { throw new NullReferenceException(paramName + " cannot be null"); } return parameter; } public static string ThrowIfEmptyString(string parameter, string paramName) { if (string.IsNullOrEmpty(parameter)) { throw new NullReferenceException(paramName + " cannot be an empty string"); } return parameter; } public static string ThrowIfDirectoryNotFound(string folderPath) { if (!Directory.Exists(folderPath)) { throw new DirectoryNotFoundException(string.Format("The directory \"{0}\" does not exist", folderPath)); } return folderPath; } public static void ThrowIfFileNotFound(string filePath) { if (!File.Exists(filePath)) { throw new DirectoryNotFoundException(string.Format("The file \"{0}\" does not exist", filePath)); } } public static void ThrowIfZeroOrNegative(int parameter, string paramName) { if (parameter < 1) { throw new DirectoryNotFoundException(string.Format("The parameter \"{0}\" must be greater than 0", paramName)); } } public static void Throw(string format, params object[] args) where T : Exception { DebugUtility.CheckDebuggerBreak(); T exception = (T)global::System.Activator.CreateInstance(typeof(T), string.Format(format, args)); throw exception; } public static void Throw(Exception innerException, string format, params object[] args) where T : Exception { DebugUtility.CheckDebuggerBreak(); T exception = (T)global::System.Activator.CreateInstance(typeof(T), string.Format(format, args), innerException); throw exception; } private static string ToExceptionString(Exception inException, bool includeAll) { StringBuilder sb = new StringBuilder(); if (inException != null) { AppendExceptionString(inException, string.Empty, sb, includeAll); if (inException.InnerException != null) { Exception curException = inException.InnerException; string prefix = "\t"; do { sb.AppendLine(" "); sb.AppendLine(" "); sb.AppendLine(prefix + "INNER EXCEPTION: "); sb.AppendLine(" "); AppendExceptionString(curException, prefix, sb, includeAll); curException = curException.InnerException; prefix += "\t"; } while (curException != null); } } return sb.ToString(); } private static void AppendExceptionString(Exception inException, string inPrefix, StringBuilder ioSB, bool includeAll) { if (includeAll) { ioSB.Append(inPrefix); ioSB.AppendFormat("Type: {0}", inException.GetType().ToString()); ioSB.AppendLine(" "); ioSB.Append(inPrefix); ioSB.AppendFormat("Message: {0}", inException.Message); ioSB.AppendLine(" "); ioSB.Append(inPrefix); ioSB.AppendFormat("StackTrace: {0}", (inException.StackTrace != null) ? inException.StackTrace.ToString() : ""); } else { ioSB.Append(inPrefix); ioSB.AppendFormat("Message: {0}", inException.Message); } } } /// /// This exception should be thrown when a field for a class has not been /// initialized correctly. /// [Serializable] public class FieldNotInitializedException : ArgumentException { public FieldNotInitializedException() { } public FieldNotInitializedException(string message) : base(message) { } public FieldNotInitializedException(string message, string paramName) : base(message, paramName) { } public FieldNotInitializedException(string message, Exception innerException) : base(message, innerException) { } public FieldNotInitializedException(string message, string paramName, Exception innerException) : base(message, paramName, innerException) { } protected FieldNotInitializedException(SerializationInfo info, StreamingContext context) : base(info, context) { } public static void ThrowIfNull(object owner, object field, string fieldName, bool doDebuggerBreak) { if (field == null) { DebugUtility.CheckDebuggerBreak(doDebuggerBreak); throw new FieldNotInitializedException("The field \"" + owner.GetType().FullName + "." + fieldName + "\" is null.", fieldName); } } public static void ThrowIfNull(object owner, object field, string fieldName) { ThrowIfNull(owner, field, fieldName, true); } public static void ThrowIfNull(object owner, ref T field) where T : class { if (field == null) { ThrowIfNull(owner, field, GetPropertyName(owner, ref field)); } } public static void ThrowIfRelativeFileMissing(string fileRelativePath) { ThrowIfRelativeFileMissing(fileRelativePath, true); } public static void ThrowIfDirectoryDoesNotExist(string dirPath) { if (!Directory.Exists(dirPath)) { DebugUtility.CheckDebuggerBreak(); throw new DirectoryNotFoundException(string.Format("Directory not found: \"{0}\"", dirPath)); } } public static void ThrowIfRelativeFileMissing(string fileRelativePath, bool doDebuggerBreak) { string path = FileUtility.PathFromExecutingAssemblyRelativePath(fileRelativePath); if (!File.Exists(path)) { DebugUtility.CheckDebuggerBreak(doDebuggerBreak); throw new FileNotFoundException("File is missing", path); } } public static void ThrowIfEmptyString(object owner, string field, string fieldName, bool doDebuggerBreak) { if (string.IsNullOrEmpty(field)) { DebugUtility.CheckDebuggerBreak(doDebuggerBreak); throw new FieldNotInitializedException("The string field \"" + owner.GetType().FullName + "." + fieldName + "\" is null or empty.", fieldName); } } public static void ThrowIfEmptyString(object owner, string field, string fieldName) { ThrowIfNull(owner, field, fieldName, true); } public static void ThrowIfEmptyString(object owner, ref string field) { if (string.IsNullOrEmpty(field)) { ThrowIfEmptyString(owner, field, GetPropertyName(owner, ref field)); } } public static void ThrowIfZero(object owner, ref int field) { if (field == 0) { ThrowIfNull(owner, field, GetPropertyName(owner, ref field)); } } private static string GetPropertyName(object owner, ref T field) { string name = string.Empty; IList fields = ReflectionUtility.GetAllPublicAndPrivateInstanceFields(owner.GetType()); foreach (FieldInfo curField in fields) { if (object.ReferenceEquals(curField.GetValue(owner), field)) { name = curField.Name; } } return name; } } [Serializable] public class FatalAppException : ApplicationException { public FatalAppException() { } public FatalAppException(string message) : base(message) { } public FatalAppException(string message, Exception innerException) : base(message, innerException) { } protected FatalAppException(SerializationInfo info, StreamingContext context) : base(info, context) { } } [Serializable] public class ArgException : ArgumentException { public ArgException(string format, params object[] args) : base(string.Format(format, args)) { DebugUtility.CheckDebuggerBreak(); } public ArgException(Exception innerException, string format, params object[] args) : base(string.Format(format, args), innerException) { DebugUtility.CheckDebuggerBreak(); } protected ArgException(SerializationInfo info, StreamingContext context) : base(info, context) { } } [Serializable] public class UniqueIdentifierException : ApplicationException { public UniqueIdentifierException() { } public UniqueIdentifierException(string message) : base(message) { } protected UniqueIdentifierException(SerializationInfo info, StreamingContext context) : base(info, context) { } } }