rspec/rules/S4027/csharp/rule.adoc
2024-01-25 18:01:26 +00:00

57 lines
1.4 KiB
Plaintext

== Why is this an issue?
Exceptions types should provide the following constructors:
* `public MyException()`
* `public MyException(string)`
* `public MyException(string, Exception)`
The absence of these constructors can complicate exception handling and limit the information that can be provided when an exception is thrown.
== How to fix it
=== Code examples
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
----
public class MyException : Exception // Noncompliant: several constructors are missing
{
public MyException()
{
}
}
----
==== Compliant solution
[source,csharp,diff-id=1,diff-type=compliant]
----
public class MyException : Exception
{
public MyException()
{
}
public MyException(string message)
: base(message)
{
}
public MyException(string message, Exception innerException)
: base(message, innerException)
{
}
}
----
== 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[]