2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-04-28 16:49:39 +02:00
Some constructors of the ``++ArgumentException++``, ``++ArgumentNullException++``, ``++ArgumentOutOfRangeException++`` and ``++DuplicateWaitObjectException++`` classes must be fed with a valid parameter name. This rule raises an issue in two cases:
* When this parameter name doesn't match any existing ones.
2023-12-14 15:13:03 +01:00
* When a call is made to the default (parameterless) constructor
2021-04-28 16:49:39 +02:00
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2021-04-28 16:49:39 +02:00
----
2023-12-14 15:13:03 +01:00
public void Foo(Bar a, int[] b)
2021-04-28 16:49:39 +02:00
{
2023-12-14 15:13:03 +01:00
throw new ArgumentException(); // Noncompliant
throw new ArgumentException("My error message", "c"); // Noncompliant
throw new ArgumentException("My error message", "c", innerException); // Noncompliant
throw new ArgumentNullException("c"); // Noncompliant
throw new ArgumentNullException(nameof(c)); // Noncompliant
throw new ArgumentNullException("My error message", "a"); // Noncompliant
throw new ArgumentOutOfRangeException("c"); // Noncompliant
throw new ArgumentOutOfRangeException("c", "My error message"); // Noncompliant
throw new ArgumentOutOfRangeException("c", b, "My error message"); // Noncompliant
throw new DuplicateWaitObjectException("c", "My error message"); // Noncompliant
2021-04-28 16:49:39 +02:00
}
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2021-04-28 16:49:39 +02:00
----
2023-12-14 15:13:03 +01:00
public void Foo(Bar a, int[] b)
2021-04-28 16:49:39 +02:00
{
throw new ArgumentException("My error message", "a");
2023-12-14 15:13:03 +01:00
throw new ArgumentException("My error message", "b", innerException);
2021-04-28 16:49:39 +02:00
throw new ArgumentNullException("a");
throw new ArgumentNullException(nameof(a));
throw new ArgumentNullException("a", "My error message");
2023-12-14 15:13:03 +01:00
2021-04-28 16:49:39 +02:00
throw new ArgumentOutOfRangeException("b");
throw new ArgumentOutOfRangeException("b", "My error message");
throw new ArgumentOutOfRangeException("b", b, "My error message");
2023-12-14 15:13:03 +01:00
2021-04-28 16:49:39 +02:00
throw new DuplicateWaitObjectException("b", "My error message");
}
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Exceptions
2021-04-28 16:49:39 +02:00
2023-12-14 15:13:03 +01:00
The rule won't raise an issue if the parameter name is not a constant value.
2021-04-28 18:08:03 +02:00
2021-09-20 15:38:42 +02:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
* Use a constructor overload that allows a more meaningful exception message to be provided.
* The parameter name '{0}' is not declared in the argument list.
* ArgumentException constructor arguments have been inverted.
=== Highlighting
The string literal supposed to be the parameter name
2021-09-20 15:38:42 +02:00
endif::env-github,rspecator-view[]