Skip to content

moerwald/Exceptions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Quality Gate Status Code Smells Lines of Code Maintainability Rating Security Rating BCH compliance

Exceptions

This module offers the following exception helpers:

  • Boundary
  • Convert
  • Ignore

Boundary

Since a lot of exceptions are undocumented in the NET framework or third party libraries code ends up with a lot of

try{

}
catch (Exception ex){

}

blocks. In my oppionion these blocks should only work as exception boundary. Since it is unclear if a try-catch-anything-block works as exception boundary, an explicit "statement" would be an improvement. The static Boundary class offers such an statement:

    [Test]
    public void CatchDoesntPopulateException()
    {
      Assert.DoesNotThrow(
        () => Boundary.CatchAll(
          () => throw new Exception("Test")
        )
      );
    }

As the name says CatchAll catches all kind of exceptions. CatchAll accepts an additional (optional) delegate populating the cought exception to e.g. log it to a file.

Convert

The Convert class translates ExceptionA to ExceptionB. This behaviour is usefull to translate a standard exception to a custom one. Example: You've a config class, every error during config loading, parsing, validating shall be communicated via an ConfigException. Based on that you're able to e.g. wrap a FileNotFound-exception in a ConfigException (where the inner exeption is FileNotFound).

Example:

        [Test]
        public void ConvertExceptionToMyCustomException()
        {
            var caughtException = Assert.Throws<MyCustomException>(() =>
            {
                Exceptions.Convert.To<ArgumentNullException, MyCustomException>(() => throw new ArgumentNullException("Test"));
            });

            Assert.Multiple(() =>
            {
                Assert.That(caughtException.InnerException, Is.Not.Null);
                Assert.That(caughtException.InnerException.GetType(), Is.EqualTo(typeof(ArgumentNullException)));
            });
        }

Ignore

Sometimes you only want to work with return types, and don't bother any exception. The Ignore class offers an AllExceptions and SomeExceptions method (which are self-explanatory).

        [Test]
        public void IgnoreExceptionAndReturnNativeType()
        {
            Assert.DoesNotThrow(() =>
            {
                var result = Ignore.AllExceptions<bool>(() => throw new Exception("Test"));
                Assert.That(result, Is.False);
            });
        }

In case of an exception AllExceptions<bool> returns the default value of the given generic type (in case of bool false). As other methods AllExceptions<bool> offers an delegate to e.g. log the occurred exceptions. Ignoreclass also offers a SomeExceptionsmethod:

  [Test]
        public void IgnoreSomeExceptionsCaughtExceptionIsIgnored()
        {
            Assert.DoesNotThrow(
                () =>
                {
                    var result = Ignore.SomeExceptions<bool, ArgumentException, NullReferenceException, FormatException>(() =>
                    {
                        throw new ArgumentException("Test");
                    });

                    Assert.That(result, Is.False);
                }
                );
        }

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages