Throwing Custom exceptions #181
Unanswered
lars-lindstrom
asked this question in
Q&A
Replies: 1 comment
-
I would use the extension approach shown in the project README. Don't just call it https://ardalis.com/prefer-custom-exceptions-to-framework-exceptions/ Extending with your own Guard ClausesTo extend your own guards, you can do the following: // Using the same namespace will make sure your code picks up your
// extensions no matter where they are in your codebase.
namespace Ardalis.GuardClauses
{
public static class FooGuard
{
public static void Foo(this IGuardClause guardClause, string input, string parameterName)
{
if (input?.ToLower() == "foo")
throw new ArgumentException("Should not have been foo!", parameterName);
}
}
}
// Usage
public void SomeMethod(string something)
{
Guard.Against.Foo(something, nameof(something));
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have previously used the Ardalis Specification/Repository assembly with great pleasure and currently the GuardClause assembly got my attention.
In general it is great and I love it. But listening to your podcasts, inspired me to use custom exceptions to a greater extend. Unfortunately I have fails to identify a way to either use functionality within Ardalis GuardClauses or to make and overloaded version myself.
What I’m looking for I something like this: Guard.Agains.Null(null object)
Beta Was this translation helpful? Give feedback.
All reactions