I'm trying to create a custom exception class in C# 12.0 (.NET 8.0, SDK Version: 8.0.100-rc.2.23502.2) that supports a constructor with an inner exception parameter. Here's the code I initially tried:
public class TokenAcquisitionException(string message) : Exception(message)
{
private const string FailedToGetToken = "Failed to get token";
public TokenAcquisitionException() : this(FailedToGetToken)
{
}
public TokenAcquisitionException(string message, Exception innerException) : base(message, innerException)
{
/* What do I put here? */
}
}
However, when I try to compile this code, I get the following error:
Error CS8862 A constructor declared in a type with parameter list must have 'this' constructor initializer.
I found a GitHub issue that discusses a similar error, but
it's related to records in C# 9 and later, not classes. The issue
suggests that every constructor in a record with a primary constructor
must call the primary constructor using this
, otherwise the
compiler wouldn't know how to handle the primary constructor
parameters.
I tried various approaches to resolve this issue, including creating a
custom exception like TokenAcquisitionException
and
using this
instead of base
in the constructor.
public class TokenAcquisitionException(string message) : Exception(message)
{
private const string FailedToGetToken = "Failed to get token";
public TokenAcquisitionException() : this(FailedToGetToken)
{
}
public TokenAcquisitionException(string message, Exception innerException) : this(message)
{
/* What do I put here? */
}
}
However, none of these approaches worked.
Does anyone know how to correctly implement a constructor with an inner exception parameter in a C# class? Any help would be greatly appreciated.
I was expecting that I could call the base class constructor with the same constructor signature:
base(message, innerException)
Typically, we can invoke the analogous constructor to pass responsibility for instantiation to the base class when the base class already significantly sets up the object.
Yes, this applies to all primary constructors, now that primary constructors are available for classes too.
Just make the inner exception constructor your primary constructor instead.