Skip to content
Marek Fišera edited this page Aug 7, 2015 · 4 revisions

Exceptions defines three types of method parameters checking and ensuring its values. These are defined in reusable manner. All methods are defined on static class Ensure, which is placed inside root namespace. So this class is simply visible in all Neptuo projects without requiring any using (because all Neptuo projects are placed in sub-namespace of Neptuo).

Base parameter checking

Directly on Ensure there are defined static method for base parameter checking. These include NotNull, NotNullOrEmpty, positive, negative or zero and all combinations of these. These methods are used very offset and the set is nearly closed.

These methods provides validation rules and throws exception if this validation is not satified. E.g:

public void SaveText(string text)
{
    Ensure.NotNullOrEmpty(text, "text");

    ...
}

Exception extensions

Class Ensure defines helper object Exception on which are defined extension methods for throwing exceptions. All methods here should return instance of exception. Example usage:

public void CreateFile(string filePath)
{
    if(File.Exists(filePath))
        throw Ensure.Exception.ArgumentOutOfRange("filePath", "Argument '{0}' must be valid path to not-existing path", filePath);

    ...
}

Condition extensions

Like for 'exception extensions', class Ensure defines helper object for defining more advanced (in comparison to NotNull and others) parameter validation methods. For e.g., in file system, there are methods for requiring valid file and directory paths. These methods can be module/library specific, so they are defines using extension methods. Like static methods on Ensure, these also provides some validation logic and if it is not satisfied, exception is thrown. For e.g.:

public string ReadFile(string filePath)
{
    Ensure.Condition.FileExists(filePath, "filePath");

    ...
}
Clone this wiki locally