RG.AutoException
2.0.1
dotnet add package RG.AutoException --version 2.0.1
NuGet\Install-Package RG.AutoException -Version 2.0.1
<PackageReference Include="RG.AutoException" Version="2.0.1" />
<PackageVersion Include="RG.AutoException" Version="2.0.1" />
<PackageReference Include="RG.AutoException" />
paket add RG.AutoException --version 2.0.1
#r "nuget: RG.AutoException, 2.0.1"
#:package RG.AutoException@2.0.1
#addin nuget:?package=RG.AutoException&version=2.0.1
#tool nuget:?package=RG.AutoException&version=2.0.1
RG.AutoException
Generate exceptions as you type.
Just type:
throw new StupidUserException();
And the following code will be generated:
using System;
namespace GeneratedExceptions
{
public sealed class StupidUserException : Exception
{
public StupidUserException() : base() { }
public StupidUserException(string message) : base(message) { }
public StupidUserException(string message, Exception innerException) : base(message, innerException) { }
}
}
All three standard exception constructors are generated, allowing you to use any of these patterns:
throw new StupidUserException();
throw new StupidUserException("Something went wrong");
throw new StupidUserException("Something went wrong", innerException);
Init-Only Properties
You can also use object initializer syntax to add custom properties to your exceptions:
throw new StupidUserException
{
Name = "Bambang"
};
This will generate a nullable init-only property:
using System;
namespace GeneratedExceptions
{
public sealed class StupidUserException : Exception
{
public StupidUserException() : base() { }
public StupidUserException(string message) : base(message) { }
public StupidUserException(string message, Exception innerException) : base(message, innerException) { }
public string? Name { get; init; }
}
}
Supported Property Types
Only primitive types are supported for init-only properties:
stringint,long,short,byte,sbyteuint,ulong,ushortfloat,double,decimalboolcharGuidDateTime,DateTimeOffset,TimeSpan
Multiple Properties
Properties are merged from all usages of the same exception type:
// First usage
throw new UserException { Name = "Test" };
// Second usage (somewhere else in the code)
throw new UserException { Age = 25 };
// Generated exception will have both properties:
// public string? Name { get; init; }
// public int? Age { get; init; }
Conflicting Property Types
If the same property is used with different types across usages, a ConflictingType placeholder type is used for the property:
// First usage
throw new UserException { Id = "bambang" };
// Second usage
throw new UserException { Id = 1024 };
// Generated exception will have:
// public ConflictingType? Id { get; init; }
Since ConflictingType doesn't exist, this will result in a compilation error, prompting you to use consistent types for the property.
Specifying Base Exception Class
By default, generated exceptions inherit from Exception. You can specify a different base class using an explicit cast expression:
throw (ArgumentException)new StupidUserException("That's not how you use it", nameof(name));
This will generate an exception that inherits from ArgumentException:
using System;
namespace GeneratedExceptions
{
public sealed class StupidUserException : ArgumentException
{
public StupidUserException() : base() { }
public StupidUserException(string? message) : base(message) { }
public StupidUserException(string? message, Exception? innerException) : base(message, innerException) { }
public StupidUserException(string? message, string? paramName) : base(message, paramName) { }
public StupidUserException(string? message, string? paramName, Exception? innerException) : base(message, paramName, innerException) { }
}
}
The generated exception will include all public constructors from the specified base class.
Combining Base Class with Properties
You can combine base class specification with init-only properties:
throw (ArgumentException)new DetailedArgumentException("error", "param")
{
FieldName = "username",
AttemptedValue = 42
};
Conflicting Base Class Specifications
If the same exception is used with different base classes, a ConflictingType placeholder is used:
// First usage
throw (ArgumentException)new ConflictBaseException();
// Second usage
throw (InvalidOperationException)new ConflictBaseException();
// Generated exception will have:
// public sealed class ConflictBaseException : ConflictingType { }
Since ConflictingType doesn't exist, this will result in a compilation error, prompting you to use a consistent base class.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.