ThrowNewAttribute Class
Specifies that the source generator of L.o.U.I.S. adds ThrowNew methods for a constructor to the exception class that declares the constructor.
[AttributeUsage(AttributeTargets.Constructor, Inherited = false)]
public sealed class ThrowNewAttribute : AttributeRemarks
For a constructor with this attribute, the source generator adds two static methods to the class. Both methods have the parameters and the accessibility of the constructor, and throw a new instance of the class that the constructor initializes.
The first method returns no value. Call it as a statement.
The second method has a type parameter, which is also its return type. Call it where the code needs an expression of that type.
The class must be partial, must not be abstract, and must derive from Exception. If the class is nested, the types that contain it must be partial too.
For a primary constructor, apply this attribute to the class declaration with the method target, as in [method: ThrowNew].
The code that the source generator adds needs C# 8 or later.
Example
The following code declares an exception class, and throws an instance of it:
public sealed partial class QuotaExceededException : Exception
{
[ThrowNew]
public QuotaExceededException(string message)
: base(message)
{
}
}
// As a statement
QuotaExceededException.ThrowNew("The quota is used up.");
// In an expression
return quota ?? QuotaExceededException.ThrowNew<Quota>("The account has no quota.");See also
- Inheritance
AttributeThrowNewAttribute
Constructors
| Name | Description |
|---|---|
| ThrowNewAttribute() | Initializes a new instance of the ThrowNewAttribute class. |
ThrowNewAttribute()
Initializes a new instance of the ThrowNewAttribute class.
public ThrowNewAttribute()