Skip to content

Instantly share code, notes, and snippets.

@kmorcinek
Created November 17, 2017 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmorcinek/7e872d1fccc1771d9ef39cb83332b25a to your computer and use it in GitHub Desktop.
Save kmorcinek/7e872d1fccc1771d9ef39cb83332b25a to your computer and use it in GitHub Desktop.
EnumGuard for better exception throwing when code have not handled enum values
using System;
using JetBrains.Annotations;
public class EnumGuard
{
[Pure]
public static ArgumentOutOfRangeException CreateMissingEnumException<T>(string paramName, T value)
where T : struct
{
return new ArgumentOutOfRangeException(
paramName,
value,
$"Enum '{typeof(T).Name}' has invalid value '{value}'");
}
[Pure]
public static ArgumentOutOfRangeException CreateMissingEnumException<T>(string paramName, T? value)
where T : struct
{
return new ArgumentOutOfRangeException(
paramName,
value,
$"Nullable enum '{typeof(T).Name}' has invalid value '{value}'");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment