Skip to content

Commit

Permalink
Add UnreachableException, which was introduced in .NET 7 (#78)
Browse files Browse the repository at this point in the history
* Add `UnreachableException`, which was introduced in .NET 7

* Added test to check targetframework compatiblity
  • Loading branch information
samtrion authored Aug 28, 2023
1 parent ec2ec72 commit dcf3172
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/Polyfill/UnreachableException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// <auto-generated />

#if !NET7_0_OR_GREATER

#nullable enable

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;

namespace System.Diagnostics;

/// <summary>
/// Exception thrown when the program executes an instruction that was thought to be unreachable.
/// </summary>
/// <seealso href="https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.unreachableexception"/>
/// <seealso href="https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/Diagnostics/UnreachableException.cs"/>
[ExcludeFromCodeCoverage]
[DebuggerNonUserCode]
#if PolyPublic
public
#endif
sealed class UnreachableException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="UnreachableException"/> class with the default error message.
/// </summary>
public UnreachableException()
: base("The program executed an instruction that was thought to be unreachable.")
{
}

/// <summary>
/// Initializes a new instance of the <see cref="UnreachableException"/>
/// class with a specified error message.
/// </summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
public UnreachableException(string? message)
: base(message)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="UnreachableException"/>
/// class with a specified error message and a reference to the inner exception that is the cause of
/// this exception.
/// </summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception.</param>
public UnreachableException(string? message, Exception? innerException)
: base(message, innerException)
{
}
}

#endif
11 changes: 11 additions & 0 deletions src/Tests/UnreachableExceptionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Tests;

[TestFixture]
public class UnreachableExceptionTests
{
[Test]
public void UnreachableException_Compatiblity_with_all_TargetFrameworks()
{
_ = Assert.Throws<UnreachableException>(() => throw new UnreachableException());
}
}

0 comments on commit dcf3172

Please sign in to comment.