Skip to content

Commit

Permalink
Add SwitchExpressionException
Browse files Browse the repository at this point in the history
Fixes: #33284
  • Loading branch information
maryamariyan committed Jan 30, 2019
1 parent ba56812 commit 79ae1b3
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1842,6 +1842,17 @@ public static class ProfileOptimization
public static void StartProfile(string profile) { throw null; }
}
}
namespace System.Runtime.CompilerServices
{
[Serializable]
public sealed class SwitchExpressionException : System.InvalidOperationException
{
public SwitchExpressionException() { }
public SwitchExpressionException(object unmatchedValue) { }
public object UnmatchedValue { get { throw null; } }
public override void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
}
}
namespace System.Runtime.Versioning
{
public sealed partial class FrameworkName : System.IEquatable<System.Runtime.Versioning.FrameworkName>
Expand Down
3 changes: 3 additions & 0 deletions src/System.Runtime.Extensions/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@
<data name="Arg_AppDomainUnloadedException" xml:space="preserve">
<value>Attempted to access an unloaded AppDomain.</value>
</data>
<data name="Arg_SwitchExpressionException" xml:space="preserve">
<value>Non-exhaustive switch expression failed to match its input.</value>
</data>
<data name="ZeroLengthString" xml:space="preserve">
<value>String cannot have zero length.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<Compile Include="System\IO\InvalidDataException.cs" />
<Compile Include="System\Net\WebUtility.cs" />
<Compile Include="System\Reflection\AssemblyNameProxy.cs" />
<Compile Include="System\Runtime\CompilerServices\SwitchExpressionException.cs" />
<Compile Include="System\Runtime\ProfileOptimization.cs" />
<Compile Include="System\Runtime\Versioning\FrameworkName.cs" />
<Compile Include="System\Runtime\Versioning\ComponentGuaranteesAttribute.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.Serialization;

namespace System.Runtime.CompilerServices
{
/// <summary>
/// Indicates that a switch expression that was non-exhaustive failed to match its input
/// at runtime, e.g. in the C# 8 expression <code>3 switch { 4 => 5 }</code>.
/// The exception optionally contains an object representing the unmatched value.
/// </summary>
[Serializable]
[System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public sealed class SwitchExpressionException : InvalidOperationException//, ISerializable
{
public SwitchExpressionException()
: base(SR.Arg_SwitchExpressionException) { }

public SwitchExpressionException(object unmatchedValue)
: base()
{
UnmatchedValue = unmatchedValue;
}

private SwitchExpressionException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
info.AddValue(nameof(UnmatchedValue), UnmatchedValue, typeof(object));
}

public object UnmatchedValue { get; }

public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue(nameof(UnmatchedValue), UnmatchedValue, typeof(object));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<Compile Include="System\Diagnostics\Stopwatch.cs" />
<Compile Include="System\Environment.MachineName.cs" />
<Compile Include="System\IO\PathTests_Combine.cs" />
<Compile Include="System\Runtime\CompilerServices\SwitchExpressionExceptionTests.cs" />
<Compile Include="System\Runtime\Versioning\FrameworkName.cs" />
<Compile Include="System\IO\PathTestsBase.cs" />
<Compile Include="System\Net\WebUtility.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.CompilerServices;
using Xunit;

namespace System.Runtime.CompilerServices.Tests
{
public class SwitchExpressionExceptionTests
{
[Fact]
public void UnmatchedValue()
{
var ex = new SwitchExpressionException(34);
Assert.Equal(34, ex.UnmatchedValue);

var data = new byte[] { 1, 2, 3 };
ex = new SwitchExpressionException(data);
Assert.Equal(data, ex.UnmatchedValue);

ex = new SwitchExpressionException(true);
Assert.Equal(true, ex.UnmatchedValue);

ex = new SwitchExpressionException("34");
Assert.Equal("34", ex.UnmatchedValue);
}
}
}

0 comments on commit 79ae1b3

Please sign in to comment.