Improve OutOfRangeException message

This commit is contained in:
Deukhoofd 2024-07-28 12:57:01 +02:00
parent 10f411f076
commit 3d5fb1a818
2 changed files with 13 additions and 1 deletions

View File

@ -6,9 +6,20 @@ namespace PkmnLib.Static.Utils.Errors;
/// </summary> /// </summary>
public class OutOfRangeException : Exception public class OutOfRangeException : Exception
{ {
private readonly string _hint;
private readonly int _index;
private readonly int _max;
/// <inheritdoc cref="OutOfRangeException"/> /// <inheritdoc cref="OutOfRangeException"/>
public OutOfRangeException(string hint, int index, int max) public OutOfRangeException(string hint, int index, int max)
: base($"{hint} index {index} is out of range. Must be between 0 and {max - 1}.")
{ {
_hint = hint;
_index = index;
_max = max;
} }
/// <inheritdoc />
public override string Message => _max == 0
? $"{_hint} index {_index} is out of range. Collection is empty."
: $"{_hint} index {_index} is out of range. Must be between 0 and {_max - 1}.";
} }

View File

@ -4,6 +4,7 @@
<TargetFramework>netstandard2.1</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<LangVersion>12</LangVersion> <LangVersion>12</LangVersion>
<WarningsAsErrors>nullable</WarningsAsErrors>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>