RG.AutoException 2.0.1

dotnet add package RG.AutoException --version 2.0.1
                    
NuGet\Install-Package RG.AutoException -Version 2.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RG.AutoException" Version="2.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RG.AutoException" Version="2.0.1" />
                    
Directory.Packages.props
<PackageReference Include="RG.AutoException" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RG.AutoException --version 2.0.1
                    
#r "nuget: RG.AutoException, 2.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package RG.AutoException@2.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RG.AutoException&version=2.0.1
                    
Install as a Cake Addin
#tool nuget:?package=RG.AutoException&version=2.0.1
                    
Install as a Cake Tool

RG.AutoException

NuGet .NET

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:

  • string
  • int, long, short, byte, sbyte
  • uint, ulong, ushort
  • float, double, decimal
  • bool
  • char
  • Guid
  • DateTime, 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.

There are no supported framework assets in this package.

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.

Version Downloads Last Updated
2.0.1 123 11/29/2025
1.0.1 523 11/22/2022
1.0.0 465 11/21/2022