Table of Contents

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

argName string
message string

Returns

bool

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

arrayName string
dex int
valueElement string
unallowedStrings string[]

Returns

bool

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

elVal object
listName string
SH_NullToStringOrDefault Func<object, string>

Returns

bool

BadMappedXaml(string, string)

Throws an exception if XAML is badly mapped.

public static bool BadMappedXaml(string nameControl, string additionalInfo)

Parameters

nameControl string
additionalInfo string

Returns

bool

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

year int
month int
day int
hour int
minute int
seconds int
ex Exception

Returns

bool

CannotMoveFolder(string, string, Exception)

Throws an exception if a folder cannot be moved.

public static bool CannotMoveFolder(string sourcePath, string destinationPath, Exception ex)

Parameters

sourcePath string
destinationPath string
ex Exception

Returns

bool

CheckBackslashEnd(string)

Throws an exception if a string doesn't end with a backslash.

public static bool CheckBackslashEnd(string path)

Parameters

path string

Returns

bool

Custom(Exception, bool)

Throws a custom exception from an Exception object.

public static bool Custom(Exception ex, bool reallyThrow = true)

Parameters

ex Exception
reallyThrow bool

Returns

bool

Custom(string, bool, string)

Throws a custom exception with the specified message.

public static bool Custom(string message, bool reallyThrow = true, string secondMessage = "")

Parameters

message string
reallyThrow bool
secondMessage string

Returns

bool

CustomWithStackTrace(Exception)

Throws a custom exception with stack trace from an Exception object.

public static bool CustomWithStackTrace(Exception ex)

Parameters

ex Exception

Returns

bool

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

firstCollectionName string
firstCollectionCount int
secondCollectionName string
secondCollectionCount int

Returns

bool

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

firstCollectionName string
firstCollection IList<T>
secondCollectionName string
secondCollection IList<U>

Returns

bool

Type Parameters

T
U

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

firstCollectionName string
firstCollection IList<T>
secondCollectionName string
secondCollection IList<T>

Returns

bool

Type Parameters

T

DirectoryExists(string)

Throws an exception if a directory doesn't exist.

public static bool DirectoryExists(string path)

Parameters

path string

Returns

bool

DirectoryWasntFound(string)

Throws an exception if a directory wasn't found.

public static bool DirectoryWasntFound(string directory)

Parameters

directory string

Returns

bool

DivideByZero()

Throws an exception for division by zero.

public static bool DivideByZero()

Returns

bool

DoesntHaveRequiredType(string)

Throws an exception if a variable doesn't have the required type.

public static bool DoesntHaveRequiredType(string variableName)

Parameters

variableName string

Returns

bool

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

nameOfVariable string
duplicatedElements List<string>
message string

Returns

bool

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

nameCollection string
element string

Returns

bool

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

detailLocation string
countBefore int
countAfter int

Returns

bool

ExcAsArg(Exception, string)

Throws an exception with exception information as argument.

public static bool ExcAsArg(Exception ex, string message = "")

Parameters

ex Exception
message string

Returns

bool

FileAlreadyExists(string)

Throws an exception if a file already exists.

public static bool FileAlreadyExists(string path)

Parameters

path string

Returns

bool

FileDoesntExists(string)

Throws an exception if a file doesn't exist.

public static bool FileDoesntExists(string fulLPath)

Parameters

fulLPath string

Returns

bool

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

fnOri string

Returns

bool

FileSystemException(Exception)

Throws a file system exception.

public static bool FileSystemException(Exception ex)

Parameters

ex Exception

Returns

bool

FirstLetterIsNotUpper(string)

Throws an exception if the first letter is not uppercase.

public static bool FirstLetterIsNotUpper(string text)

Parameters

text string

Returns

bool

FolderCannotBeDeleted(string, Exception)

Throws an exception if a folder cannot be deleted.

public static bool FolderCannotBeDeleted(string folder, Exception ex)

Parameters

folder string
ex Exception

Returns

bool

FolderCantBeRemoved(string)

Throws an exception if a folder can't be removed.

public static bool FolderCantBeRemoved(string folder)

Parameters

folder string

Returns

bool

FolderDoesNotExists(string, string)

Throws an exception if a folder does not exist.

public static bool FolderDoesNotExists(string folder, string additionalInfo = "")

Parameters

folder string
additionalInfo string

Returns

bool

FolderIsNotEmpty(string, string)

Throws an exception if a folder is not empty.

public static bool FolderIsNotEmpty(string variableName, string path)

Parameters

variableName string
path string

Returns

bool

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

functionalityName string

Returns

bool

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

list IEnumerable<T>
listName string
maxRequiredIndex int

Returns

bool

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

nameDict string
qsDict IDictionary<Key, Value>
remains Key

Returns

bool

Type Parameters

Key
Value

HasOddNumberOfElements(string, ICollection)

Throws an exception if a list has an odd number of elements.

public static bool HasOddNumberOfElements(string listName, ICollection list)

Parameters

listName string
list ICollection

Returns

bool

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

elements List<List<string>>

Returns

bool

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

variableName string
length int
requiredLenght int

Returns

bool

InvalidParameter(string, string)

Throws an exception if a parameter is invalid (URL encoded).

public static bool InvalidParameter(string valueVar, string nameVar)

Parameters

valueVar string
nameVar string

Returns

bool

IsEmpty(IEnumerable, string, string)

Throws an exception if a collection is empty.

public static bool IsEmpty(IEnumerable folders, string colName, string additionalMessage = "")

Parameters

folders IEnumerable
colName string
additionalMessage string

Returns

bool

IsNotAllowed(string)

Throws an exception if something is not allowed.

public static bool IsNotAllowed(string what)

Parameters

what string

Returns

bool

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

variableName string
variable object

Returns

bool

IsNotPositiveNumber(string, int?)

Throws an exception if a value is not a positive number.

public static bool IsNotPositiveNumber(string nameOfVariable, int? n)

Parameters

nameOfVariable string
n int?

Returns

bool

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

name1 string
value1 T
name2 string
value2 T

Returns

bool

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

argName string
argValue string
raiseIsNotWindowsPathFormat bool
SunamoFileSystem_IsWindowsPathFormat Func<string, bool>

Returns

bool

IsNull(string, object?)

Throws an exception if a variable is null.

public static bool IsNull(string variableName, object? variable = null)

Parameters

variableName string
variable object

Returns

bool

IsNullOrEmpty(string, string)

Throws an exception if a string argument is null or empty.

public static bool IsNullOrEmpty(string argName, string argValue)

Parameters

argName string
argValue string

Returns

bool

IsNullOrWhitespace(string, string)

Throws an exception if a string argument is null or whitespace.

public static bool IsNullOrWhitespace(string argName, string argValue)

Parameters

argName string
argValue string

Returns

bool

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

fst string
sec string

Returns

bool

IsWhitespaceOrNull(string, object)

Throws an exception if a value is whitespace or null.

public static bool IsWhitespaceOrNull(string variable, object data)

Parameters

variable string
data object

Returns

bool

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

input string
isWindowsPathFormat Func<string, bool>

Returns

bool

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

dictionary Dictionary<T, U>
key T
dictionaryName string

Returns

bool

Type Parameters

T
U

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

en IDictionary<T, U>
dictName string
key T

Returns

bool

Type Parameters

T
U

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

variableName string
list IEnumerable<T>

Returns

bool

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

path string
IsLockedByBitLocker Func<char, bool>

Returns

bool

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

listName string
count int
moreInfo string

Returns

bool

NameIsNotSetted(string, string)

Throws an exception if a control's name is not set.

public static bool NameIsNotSetted(string nameControl, string nameFromProperty)

Parameters

nameControl string
nameFromProperty string

Returns

bool

NoPassedFolders(ICollection)

Throws an exception if no folders were passed.

public static bool NoPassedFolders(ICollection folders)

Parameters

folders ICollection

Returns

bool

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

text string
shouldContains string[]

Returns

bool

NotExists(string)

Throws an exception if something does not exist.

public static bool NotExists(string what)

Parameters

what string

Description 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

notImplementedName object

Name 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

variableName string

Name of the variable being checked.

list IEnumerable<string>

List of string values for context.

isLt int

Lower bound threshold.

isGt int

Upper 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

what string

Description of what was expected to be an integer.

value int?

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

extension string

The 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

colName string

Name of the collection.

list ICollection

The 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

colName string

Name of the collection.

col ICollection

The collection being accessed.

indexName string

Name of the index variable.

index int

The 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

variableName string

Name of the variable.

array T[]

The array to check.

CA_IsListStringWrappedInArray Func<IEnumerable<T>, bool>

Function to check if the array wraps a list.

Returns

bool

True if exception was thrown, false otherwise.

Type Parameters

T

The 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

times int

Number of retry attempts.

timeoutInMs int

Timeout between retries in milliseconds.

address string

Address or identifier of the operation that failed.

sharedAlgorithmSlastError int

Last 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

start int

The start index.

end int

The end index.

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

input string

The input string to check.

unallowedStrings string[]

Array of unallowed substrings.

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

exception Exception

The exception object. If not null, throws an exception with its message.

reallyThrow bool

Whether 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

f Func<string, string>

Factory function that generates the exception message.

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

exception string

The exception message string. If not null, throws an exception.

reallyThrow bool

Whether 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

f Func<string, A, string>

Factory function that generates the exception message.

ex A

Argument to the factory function.

Returns

bool

True if exception was thrown, false otherwise.

Type Parameters

A

Type 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

f Func<string, A, B, string>

Factory function that generates the exception message.

ex A

First argument to the factory function.

message B

Second argument to the factory function.

Returns

bool

True if exception was thrown, false otherwise.

Type Parameters

A

Type of the first argument.

B

Type 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

url string

The URL to validate.

uhIsUri Func<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

name string

Name of the handler.

keysHandler object

The keys handler object.

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

path string

Path to the file.

requiredExt string

The required file extension.

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

requireElements int

Required number of elements.

nameCollection string

Name of the collection.

collection IEnumerable<T>

The collection to check.

Returns

bool

True if exception was thrown, false otherwise.

Type Parameters

T

The 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

nameOfVariable string

Name of the variable containing the list.

list List<string>

The list to check.

Returns

bool

True if exception was thrown, false otherwise.