Class ThrowEx
- Namespace
- SunamoExceptions
- Assembly
- SunamoExceptions.dll
Provides static methods for throwing exceptions with automatic context information. Each method validates a condition and throws an exception if the condition fails.
public class ThrowEx
- Inheritance
-
ThrowEx
- Inherited Members
- Extension Methods
Methods
ArgumentOutOfRangeException(string, string)
Throws an ArgumentOutOfRangeException with the specified parameters.
public static bool ArgumentOutOfRangeException(string argName, string message = "")
Parameters
Returns
ArrayElementContainsUnAllowedStrings(string, int, string, params string[])
Throws an exception if an array element contains unallowed strings.
public static bool ArrayElementContainsUnAllowedStrings(string arrayName, int dex, string valueElement, params string[] unallowedStrings)
Parameters
Returns
BadFormatOfElementInList(object, string, Func<object, string>)
Throws an exception if a list element has a bad format.
public static bool BadFormatOfElementInList(object elVal, string listName, Func<object, string> SH_NullToStringOrDefault)
Parameters
Returns
BadMappedXaml(string, string)
Throws an exception if XAML is badly mapped.
public static bool BadMappedXaml(string nameControl, string additionalInfo)
Parameters
Returns
CannotCreateDateTime(int, int, int, int, int, int, Exception)
Throws an exception if a DateTime cannot be created with the specified parameters.
public static bool CannotCreateDateTime(int year, int month, int day, int hour, int minute, int seconds, Exception ex)
Parameters
Returns
CannotMoveFolder(string, string, Exception)
Throws an exception if a folder cannot be moved.
public static bool CannotMoveFolder(string sourcePath, string destinationPath, Exception ex)
Parameters
Returns
CheckBackslashEnd(string)
Throws an exception if a string doesn't end with a backslash.
public static bool CheckBackslashEnd(string path)
Parameters
pathstring
Returns
Custom(Exception, bool)
Throws a custom exception from an Exception object.
public static bool Custom(Exception ex, bool reallyThrow = true)
Parameters
Returns
Custom(string, bool, string)
Throws a custom exception with the specified message.
public static bool Custom(string message, bool reallyThrow = true, string secondMessage = "")
Parameters
Returns
CustomWithStackTrace(Exception)
Throws a custom exception with stack trace from an Exception object.
public static bool CustomWithStackTrace(Exception ex)
Parameters
exException
Returns
DifferentCountInLists(string, int, string, int)
Throws an exception if two collections have different element counts.
public static bool DifferentCountInLists(string firstCollectionName, int firstCollectionCount, string secondCollectionName, int secondCollectionCount)
Parameters
firstCollectionNamestringfirstCollectionCountintsecondCollectionNamestringsecondCollectionCountint
Returns
DifferentCountInListsTU<T, U>(string, IList<T>, string, IList<U>)
Throws an exception if two lists of different types have different element counts.
public static bool DifferentCountInListsTU<T, U>(string firstCollectionName, IList<T> firstCollection, string secondCollectionName, IList<U> secondCollection)
Parameters
firstCollectionNamestringfirstCollectionIList<T>secondCollectionNamestringsecondCollectionIList<U>
Returns
Type Parameters
TU
DifferentCountInLists<T>(string, IList<T>, string, IList<T>)
Throws an exception if two lists have different element counts.
public static bool DifferentCountInLists<T>(string firstCollectionName, IList<T> firstCollection, string secondCollectionName, IList<T> secondCollection)
Parameters
firstCollectionNamestringfirstCollectionIList<T>secondCollectionNamestringsecondCollectionIList<T>
Returns
Type Parameters
T
DirectoryExists(string)
Throws an exception if a directory doesn't exist.
public static bool DirectoryExists(string path)
Parameters
pathstring
Returns
DirectoryWasntFound(string)
Throws an exception if a directory wasn't found.
public static bool DirectoryWasntFound(string directory)
Parameters
directorystring
Returns
DivideByZero()
Throws an exception for division by zero.
public static bool DivideByZero()
Returns
DoesntHaveRequiredType(string)
Throws an exception if a variable doesn't have the required type.
public static bool DoesntHaveRequiredType(string variableName)
Parameters
variableNamestring
Returns
DuplicatedElements(string, List<string>, string)
Throws an exception if a list contains duplicated elements.
public static bool DuplicatedElements(string nameOfVariable, List<string> duplicatedElements, string message = "")
Parameters
Returns
ElementCantBeFound(string, string)
Throws an exception if an element can't be found in a collection.
public static bool ElementCantBeFound(string nameCollection, string element)
Parameters
Returns
ElementWasntRemoved(string, int, int)
Throws an exception if an element wasn't removed from a collection.
public static bool ElementWasntRemoved(string detailLocation, int countBefore, int countAfter)
Parameters
Returns
ExcAsArg(Exception, string)
Throws an exception with exception information as argument.
public static bool ExcAsArg(Exception ex, string message = "")
Parameters
Returns
FileAlreadyExists(string)
Throws an exception if a file already exists.
public static bool FileAlreadyExists(string path)
Parameters
pathstring
Returns
FileDoesntExists(string)
Throws an exception if a file doesn't exist.
public static bool FileDoesntExists(string fulLPath)
Parameters
fulLPathstring
Returns
FileHasExtensionNotParseAbleToImageFormat(string)
Throws an exception if a file has an extension that cannot be parsed to image format.
public static bool FileHasExtensionNotParseAbleToImageFormat(string fnOri)
Parameters
fnOristring
Returns
FileSystemException(Exception)
Throws a file system exception.
public static bool FileSystemException(Exception ex)
Parameters
exException
Returns
FirstLetterIsNotUpper(string)
Throws an exception if the first letter is not uppercase.
public static bool FirstLetterIsNotUpper(string text)
Parameters
textstring
Returns
FolderCannotBeDeleted(string, Exception)
Throws an exception if a folder cannot be deleted.
public static bool FolderCannotBeDeleted(string folder, Exception ex)
Parameters
Returns
FolderCantBeRemoved(string)
Throws an exception if a folder can't be removed.
public static bool FolderCantBeRemoved(string folder)
Parameters
folderstring
Returns
FolderDoesNotExists(string, string)
Throws an exception if a folder does not exist.
public static bool FolderDoesNotExists(string folder, string additionalInfo = "")
Parameters
Returns
FolderIsNotEmpty(string, string)
Throws an exception if a folder is not empty.
public static bool FolderIsNotEmpty(string variableName, string path)
Parameters
Returns
FullNameOfExecutedCode()
Gets the full name of the currently executing code (type and method name).
public static string FullNameOfExecutedCode()
Returns
- string
A string in the format "FullTypeName.MethodName".
FunctionalityDenied(string)
Throws an exception if functionality is denied.
public static bool FunctionalityDenied(string functionalityName)
Parameters
functionalityNamestring
Returns
HasNotIndex<T>(IEnumerable<T>, string, int)
Throws an exception if a list doesn't have the required index.
public static bool HasNotIndex<T>(IEnumerable<T> list, string listName, int maxRequiredIndex)
Parameters
listIEnumerable<T>listNamestringmaxRequiredIndexint
Returns
Type Parameters
T
HasNotKeyDictionary<Key, Value>(string, IDictionary<Key, Value>, Key)
Throws an exception if a dictionary doesn't have the specified key.
public static bool HasNotKeyDictionary<Key, Value>(string nameDict, IDictionary<Key, Value> qsDict, Key remains)
Parameters
nameDictstringqsDictIDictionary<Key, Value>remainsKey
Returns
Type Parameters
KeyValue
HasOddNumberOfElements(string, ICollection)
Throws an exception if a list has an odd number of elements.
public static bool HasOddNumberOfElements(string listName, ICollection list)
Parameters
listNamestringlistICollection
Returns
HaveAllInnerSameCount(List<List<string>>)
Throws an exception if all inner lists don't have the same count.
public static bool HaveAllInnerSameCount(List<List<string>> elements)
Parameters
Returns
InvalidExactlyLength(string, int, int)
Throws an exception if a variable doesn't have the exact required length.
public static bool InvalidExactlyLength(string variableName, int length, int requiredLenght)
Parameters
Returns
InvalidParameter(string, string)
Throws an exception if a parameter is invalid (URL encoded).
public static bool InvalidParameter(string valueVar, string nameVar)
Parameters
Returns
IsEmpty(IEnumerable, string, string)
Throws an exception if a collection is empty.
public static bool IsEmpty(IEnumerable folders, string colName, string additionalMessage = "")
Parameters
foldersIEnumerablecolNamestringadditionalMessagestring
Returns
IsNotAllowed(string)
Throws an exception if something is not allowed.
public static bool IsNotAllowed(string what)
Parameters
whatstring
Returns
IsNotNull(string, object)
Throws an exception if a variable is not null when it should be.
public static bool IsNotNull(string variableName, object variable)
Parameters
Returns
IsNotPositiveNumber(string, int?)
Throws an exception if a value is not a positive number.
public static bool IsNotPositiveNumber(string nameOfVariable, int? n)
Parameters
Returns
IsNotTheSame<T>(string, T, string, T)
Throws an exception if two values are not the same.
public static bool IsNotTheSame<T>(string name1, T value1, string name2, T value2)
Parameters
Returns
Type Parameters
T
IsNotWindowsPathFormat(string, string, bool, Func<string, bool>)
Throws an exception if a string is not in Windows path format.
public static bool IsNotWindowsPathFormat(string argName, string argValue, bool raiseIsNotWindowsPathFormat, Func<string, bool> SunamoFileSystem_IsWindowsPathFormat)
Parameters
argNamestringargValuestringraiseIsNotWindowsPathFormatboolSunamoFileSystem_IsWindowsPathFormatFunc<string, bool>
Returns
IsNull(string, object?)
Throws an exception if a variable is null.
public static bool IsNull(string variableName, object? variable = null)
Parameters
Returns
IsNullOrEmpty(string, string)
Throws an exception if a string argument is null or empty.
public static bool IsNullOrEmpty(string argName, string argValue)
Parameters
Returns
IsNullOrWhitespace(string, string)
Throws an exception if a string argument is null or whitespace.
public static bool IsNullOrWhitespace(string argName, string argValue)
Parameters
Returns
IsTheSame(string, string)
Throws an exception if two values are the same when they should differ.
public static bool IsTheSame(string fst, string sec)
Parameters
Returns
IsWhitespaceOrNull(string, object)
Throws an exception if a value is whitespace or null.
public static bool IsWhitespaceOrNull(string variable, object data)
Parameters
Returns
IsWindowsPathFormat(string, Func<string, bool>)
Throws an exception if a string is in Windows path format when it shouldn't be.
public static bool IsWindowsPathFormat(string input, Func<string, bool> isWindowsPathFormat)
Parameters
Returns
KeyAlreadyExists<T, U>(Dictionary<T, U>, T, string)
Throws an exception if a key already exists in a dictionary.
public static bool KeyAlreadyExists<T, U>(Dictionary<T, U> dictionary, T key, string dictionaryName) where T : notnull
Parameters
dictionaryDictionary<T, U>keyTdictionaryNamestring
Returns
Type Parameters
TU
KeyNotFound<T, U>(IDictionary<T, U>, string, T)
Throws an exception if a key is not found in a dictionary.
public static bool KeyNotFound<T, U>(IDictionary<T, U> en, string dictName, T key)
Parameters
enIDictionary<T, U>dictNamestringkeyT
Returns
Type Parameters
TU
ListNullOrEmpty<T>(string, IEnumerable<T>?)
Throws an exception if a list is null or empty.
public static bool ListNullOrEmpty<T>(string variableName, IEnumerable<T>? list)
Parameters
variableNamestringlistIEnumerable<T>
Returns
Type Parameters
T
LockedByBitLocker(string, Func<char, bool>)
Throws an exception if a drive is locked by BitLocker.
public static bool LockedByBitLocker(string path, Func<char, bool> IsLockedByBitLocker)
Parameters
Returns
MoreThanOneElement(string, int, string)
Throws an exception if a list has more than one element.
public static bool MoreThanOneElement(string listName, int count, string moreInfo = "")
Parameters
Returns
NameIsNotSetted(string, string)
Throws an exception if a control's name is not set.
public static bool NameIsNotSetted(string nameControl, string nameFromProperty)
Parameters
Returns
NoPassedFolders(ICollection)
Throws an exception if no folders were passed.
public static bool NoPassedFolders(ICollection folders)
Parameters
foldersICollection
Returns
NotContains(string, params string[])
Throws an exception if a string doesn't contain required substrings.
public static bool NotContains(string text, params string[] shouldContains)
Parameters
Returns
NotExists(string)
Throws an exception if something does not exist.
public static bool NotExists(string what)
Parameters
whatstringDescription of what does not exist.
Returns
- bool
True if exception was thrown, false otherwise.
NotImplementedCase(object)
Throws an exception for a not implemented case.
public static bool NotImplementedCase(object notImplementedName)
Parameters
notImplementedNameobjectName or description of the not implemented case.
Returns
- bool
True if exception was thrown, false otherwise.
NotImplementedMethod()
Throws an exception for a not implemented method.
public static bool NotImplementedMethod()
Returns
- bool
True if exception was thrown, false otherwise.
NotInRange(string, IEnumerable<string>, int, int)
Throws an exception if a variable is not in the expected range.
public static bool NotInRange(string variableName, IEnumerable<string> list, int isLt, int isGt)
Parameters
variableNamestringName of the variable being checked.
listIEnumerable<string>List of string values for context.
isLtintLower bound threshold.
isGtintUpper bound threshold.
Returns
- bool
True if exception was thrown, false otherwise.
NotInt(string, int?)
Throws an exception if a value is not a valid integer.
public static bool NotInt(string what, int? value)
Parameters
whatstringDescription of what was expected to be an integer.
valueint?The value that failed integer validation.
Returns
- bool
True if exception was thrown, false otherwise.
NotSupported()
Throws an exception for a not supported operation.
public static bool NotSupported()
Returns
- bool
True if exception was thrown, false otherwise.
NotSupportedExtension(string)
Throws an exception for a not supported file extension.
public static bool NotSupportedExtension(string extension)
Parameters
extensionstringThe unsupported file extension.
Returns
- bool
True if exception was thrown, false otherwise.
OnlyOneElement(string, ICollection)
Throws an exception if a collection contains only one element when more were expected.
public static bool OnlyOneElement(string colName, ICollection list)
Parameters
colNamestringName of the collection.
listICollectionThe collection being checked.
Returns
- bool
True if exception was thrown, false otherwise.
OutOfRange(string, ICollection, string, int)
Throws an exception if an index is out of range for a collection.
public static bool OutOfRange(string colName, ICollection col, string indexName, int index)
Parameters
colNamestringName of the collection.
colICollectionThe collection being accessed.
indexNamestringName of the index variable.
indexintThe index that is out of range.
Returns
- bool
True if exception was thrown, false otherwise.
PassedListInsteadOfArray<T>(string, T[], Func<IEnumerable<T>, bool>)
Throws an exception if a list was passed instead of an array.
public static bool PassedListInsteadOfArray<T>(string variableName, T[] array, Func<IEnumerable<T>, bool> CA_IsListStringWrappedInArray)
Parameters
variableNamestringName of the variable.
arrayT[]The array to check.
CA_IsListStringWrappedInArrayFunc<IEnumerable<T>, bool>Function to check if the array wraps a list.
Returns
- bool
True if exception was thrown, false otherwise.
Type Parameters
TThe type of elements in the array.
RepeatAfterTimeXTimesFailed(int, int, string, int)
Throws an exception if a retry operation failed after multiple attempts.
public static bool RepeatAfterTimeXTimesFailed(int times, int timeoutInMs, string address, int sharedAlgorithmSlastError)
Parameters
timesintNumber of retry attempts.
timeoutInMsintTimeout between retries in milliseconds.
addressstringAddress or identifier of the operation that failed.
sharedAlgorithmSlastErrorintLast error code from the shared algorithm.
Returns
- bool
True if exception was thrown, false otherwise.
StartIsHigherThanEnd(int, int)
Throws an exception if start index is higher than end index.
public static bool StartIsHigherThanEnd(int start, int end)
Parameters
Returns
- bool
True if exception was thrown, false otherwise.
StringContainsUnAllowedSubstrings(string, params string[])
Throws an exception if a string contains unallowed substrings.
public static bool StringContainsUnAllowedSubstrings(string input, params string[] unallowedStrings)
Parameters
Returns
- bool
True if exception was thrown, false otherwise.
ThrowIsNotNull(Exception, bool)
Throws an exception if the exception object is not null.
public static bool ThrowIsNotNull(Exception exception, bool reallyThrow = true)
Parameters
exceptionExceptionThe exception object. If not null, throws an exception with its message.
reallyThrowboolWhether to actually throw the exception (true) or just break in debugger (false).
Returns
- bool
True if no exception was thrown, false otherwise.
ThrowIsNotNull(Func<string, string?>)
Throws an exception using a factory function with no arguments.
public static bool ThrowIsNotNull(Func<string, string?> f)
Parameters
Returns
- bool
True if exception was thrown, false otherwise.
ThrowIsNotNull(string?, bool)
Throws an exception if the exception string is not null.
public static bool ThrowIsNotNull(string? exception, bool reallyThrow = true)
Parameters
exceptionstringThe exception message string. If not null, throws an exception.
reallyThrowboolWhether to actually throw the exception (true) or just break in debugger (false).
Returns
- bool
True if exception was detected, false if exception is null.
ThrowIsNotNull<A>(Func<string, A, string?>, A)
Throws an exception using a factory function with one argument.
public static bool ThrowIsNotNull<A>(Func<string, A, string?> f, A ex)
Parameters
fFunc<string, A, string>Factory function that generates the exception message.
exAArgument to the factory function.
Returns
- bool
True if exception was thrown, false otherwise.
Type Parameters
AType of the argument.
ThrowIsNotNull<A, B>(Func<string, A, B, string?>, A, B)
Throws an exception using a factory function with two arguments.
public static bool ThrowIsNotNull<A, B>(Func<string, A, B, string?> f, A ex, B message)
Parameters
fFunc<string, A, B, string>Factory function that generates the exception message.
exAFirst argument to the factory function.
messageBSecond argument to the factory function.
Returns
- bool
True if exception was thrown, false otherwise.
Type Parameters
AType of the first argument.
BType of the second argument.
UncommentAfterNugetsFinished()
Throws an exception prompting to uncomment code after NuGet packages are finished.
public static bool UncommentAfterNugetsFinished()
Returns
- bool
True if exception was thrown, false otherwise.
UriFormat(string, Func<string, bool>)
Throws an exception if a URL has invalid URI format.
public static bool UriFormat(string url, Func<string, bool> uhIsUri)
Parameters
urlstringThe URL to validate.
uhIsUriFunc<string, bool>Function to check if the URL is a valid URI.
Returns
- bool
True if exception was thrown, false otherwise.
UseRlc()
Throws an exception prompting to use RLC (Runtime Location Cache).
public static bool UseRlc()
Returns
- bool
True if exception was thrown, false otherwise.
WasAlreadyInitialized()
Throws an exception if something was already initialized.
public static bool WasAlreadyInitialized()
Returns
- bool
True if exception was thrown, false otherwise.
WasNotKeysHandler(string, object)
Throws an exception if a keys handler is invalid.
public static bool WasNotKeysHandler(string name, object keysHandler)
Parameters
Returns
- bool
True if exception was thrown, false otherwise.
WrongExtension(string, string)
Throws an exception if a file has the wrong extension.
public static bool WrongExtension(string path, string requiredExt)
Parameters
Returns
- bool
True if exception was thrown, false otherwise.
WrongNumberOfElements<T>(int, string, IEnumerable<T>)
Throws an exception if a collection has the wrong number of elements.
public static bool WrongNumberOfElements<T>(int requireElements, string nameCollection, IEnumerable<T> collection)
Parameters
requireElementsintRequired number of elements.
nameCollectionstringName of the collection.
collectionIEnumerable<T>The collection to check.
Returns
- bool
True if exception was thrown, false otherwise.
Type Parameters
TThe type of elements in the collection.
ZeroOrMoreThanOne(string, List<string>)
Throws an exception if a list has zero or more than one element.
public static bool ZeroOrMoreThanOne(string nameOfVariable, List<string> list)
Parameters
nameOfVariablestringName of the variable containing the list.
listList<string>The list to check.
Returns
- bool
True if exception was thrown, false otherwise.