rspec/rules/S2221/csharp/rule.adoc
Fred Tingaud 51369b610e
Make sure that includes are always surrounded by empty lines (#2270)
When an include is not surrounded by empty lines, its content is inlined
on the same line as the adjacent content. That can lead to broken tags
and other display issues.
This PR fixes all such includes and introduces a validation step that
forbids introducing the same problem again.
2023-06-22 10:38:01 +02:00

76 lines
1.5 KiB
Plaintext

== Why is this an issue?
Catching `System.Exception` seems like an efficient way to handle multiple possible exceptions. Unfortunately, it traps all exception types, including the ones that were not intended to be caught. To prevent any misunderstandings, the exception filters should be used. Alternatively each exception type should be in a separate `catch` block.
=== Noncompliant code example
[source,csharp]
----
try
{
// do something that might throw a FileNotFoundException or IOException
}
catch (Exception e) // Noncompliant
{
// log exception ...
}
----
=== Compliant solution
[source,csharp]
----
try
{
// do something
}
catch (Exception e) when (e is FileNotFoundException || e is IOException)
{
// do something
}
----
=== Exceptions
The final option is to catch `System.Exception` and `throw` it in the last statement in the `catch` block. This is the least-preferred option, as it is an old-style code, which also suffers from performance penalty compared to exception filters.
[source,csharp]
----
try
{
// do something
}
catch (Exception e)
{
if (e is FileNotFoundException || e is IOException)
{
// do something
}
else
{
throw;
}
}
----
include::../see.adoc[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Catch a list of specific exception subtype or use exception filters instead.
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]