2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-04-28 16:49:39 +02:00
Exceptions types should provide the following constructors:
2024-01-18 14:09:20 +01:00
* `public MyException()`
* `public MyException(string)`
* `public MyException(string, Exception)`
2021-04-28 16:49:39 +02:00
2024-01-18 14:09:20 +01: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
2024-01-18 14:09:20 +01:00
== How to fix it
2021-04-28 16:49:39 +02:00
2024-01-18 14:09:20 +01:00
=== Code examples
2021-04-28 16:49:39 +02:00
2024-01-18 14:09:20 +01:00
==== Noncompliant code example
2021-04-28 18:08:03 +02:00
2024-01-18 14:09:20 +01:00
[source,csharp,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
2024-01-18 14:09:20 +01:00
public class MyException : Exception // Noncompliant: several constructors are missing
2021-04-28 16:49:39 +02:00
{
2024-01-25 19:01:26 +01:00
public MyException()
{
}
2021-04-28 16:49:39 +02:00
}
----
2024-01-18 14:09:20 +01:00
==== Compliant solution
2021-04-28 18:08:03 +02:00
2024-01-18 14:09:20 +01:00
[source,csharp,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
2024-01-18 14:09:20 +01:00
public class MyException : Exception
2021-04-28 16:49:39 +02:00
{
2024-01-25 19:01:26 +01: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
}
----
2021-04-28 18:08:03 +02:00
2024-01-18 14:09:20 +01:00
== Resources
2021-04-28 18:08:03 +02:00
2024-01-18 14:09:20 +01:00
=== Documentation
2021-09-20 15:38:42 +02:00
2024-01-18 14:09:20 +01:00
* 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]
2021-09-20 15:38:42 +02:00
2024-01-18 14:09:20 +01:00
include::./rspecator.adoc[]