rspec/rules/S4027/csharp/rule.adoc

57 lines
1.4 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
Exceptions types should provide the following constructors:
* `public MyException()`
* `public MyException(string)`
* `public MyException(string, Exception)`
2021-04-28 16:49:39 +02:00
The absence of these constructors can complicate exception handling and limit the information that can be provided when an exception is thrown.
2021-04-28 16:49:39 +02:00
== How to fix it
2021-04-28 16:49:39 +02:00
=== Code examples
2021-04-28 16:49:39 +02:00
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
public class MyException : Exception // Noncompliant: several constructors are missing
2021-04-28 16:49:39 +02:00
{
public MyException()
{
}
2021-04-28 16:49:39 +02:00
}
----
==== Compliant solution
[source,csharp,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
public class MyException : Exception
2021-04-28 16:49:39 +02:00
{
public MyException()
{
}
public MyException(string message)
: base(message)
{
}
public MyException(string message, Exception innerException)
: base(message, innerException)
{
}
2021-04-28 16:49:39 +02:00
}
----
== Resources
=== Documentation
* Microsoft Learn: https://learn.microsoft.com/en-us/dotnet/standard/exceptions/how-to-create-user-defined-exceptions[How to create user-defined exceptions]
* Microsoft Learn: https://learn.microsoft.com/en-us/dotnet/api/system.exception[Exception Class]
* Microsoft Learn: https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/exceptions/creating-and-throwing-exceptions#define-exception-classes[Define exception classes]
include::./rspecator.adoc[]