2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-06-01 14:44:21 +02:00
The rule is reporting when an exception is thrown from certain methods and constructors. These methods are expected to behave in a specific way and throwing an exception from them can lead to unexpected behavior and break the calling code.
2021-04-28 16:49:39 +02:00
2023-08-15 14:19:27 +02:00
[source,csharp]
2021-04-28 16:49:39 +02:00
----
public override string ToString()
{
2023-08-15 14:19:27 +02:00
if (string.IsNullOrEmpty(Name))
2021-04-28 16:49:39 +02:00
{
2023-06-01 14:44:21 +02:00
throw new ArgumentException(nameof(Name)); // Noncompliant
2021-04-28 16:49:39 +02:00
}
//...
2023-06-01 14:44:21 +02:00
}
2021-04-28 16:49:39 +02:00
----
2023-08-15 14:19:27 +02:00
An issue is raised when an exception is thrown from any of the following:
2023-06-01 14:44:21 +02:00
* https://learn.microsoft.com/en-us/dotnet/api/system.object.tostring[ToString]
* https://learn.microsoft.com/en-us/dotnet/api/system.object.equals[Object.Equals]
* https://learn.microsoft.com/en-us/dotnet/api/system.iequatable-1.equals[IEquatable.Equals]
* https://learn.microsoft.com/en-us/dotnet/api/system.object.gethashcode[GetHashCode]
* https://learn.microsoft.com/en-us/dotnet/api/system.idisposable.dispose[IDisposable.Dispose]
* https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-implement-custom-event-accessors[Event accessors]
* https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-constructors[static constructors]
* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-9.0/module-initializers[Module initializers]
* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/operator-overloading[operators ==, !=, <, >, <=, >=]
* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/user-defined-conversion-operators[implicit cast operators]
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-06-01 14:44:21 +02:00
Certain exceptions will be ignored in specific contexts, thus not raising the issue:
* `System.NotImplementedException` and its derivatives are ignored for all the aforementioned.
* `System.InvalidOperationException`, `System.NotSupportedException`, and `System.ArgumentException` and their derivatives are ignored in event accessors.
== Resources
2021-04-28 16:49:39 +02:00
2023-06-01 14:44:21 +02:00
=== Documentation
2021-04-28 18:08:03 +02:00
2023-06-01 14:44:21 +02:00
* https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/exceptions/[Exceptions and Exception Handling]
* https://learn.microsoft.com/en-us/dotnet/standard/exceptions/best-practices-for-exceptions[Best practices for exceptions]
* https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1065[CA1065: Do not raise exceptions in unexpected locations]
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Remove this "throw" statement.
=== Highlighting
``++throw xxx++``
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== on 8 Mar 2017, 14:03:45 Ann Campbell wrote:
FYI [~amaury.leve] our standard is AmE, so double quotes, not single quotes.
=== on 8 Mar 2017, 19:32:18 Ann Campbell wrote:
\[~amaury.leve] did you mean ``++Equals(Object)++`` rather than ``++Object.Equals++``?
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]