You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
KeyConverter, unlike ModifierKeysConverter and MouseActionConverter for example, does not throw NotSupportedException but throws ArgumentException instead due to the usage of Enum.Parse instead of Enum.TryParse.
This results in a dead-code being present with proper exception (key can never be null):
Description
KeyConverter
, unlikeModifierKeysConverter
andMouseActionConverter
for example, does not throwNotSupportedException
but throwsArgumentException
instead due to the usage ofEnum.Parse
instead ofEnum.TryParse
.This results in a dead-code being present with proper exception (
key
can never benull
):wpf/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Input/KeyConverter.cs
Lines 89 to 97 in cc07628
It is also the exception that is documented:
https://learn.microsoft.com/en-us/dotnet/api/system.windows.input.keyconverter.convertfrom
Reproduction Steps
Create
KeyConverter
instance, try to from string with a key that is not defined in the enum, e.g. "MyAmazingKey".Expected behavior
We will receive
NotSupportedException
withSR.Unsupported_Key
message.Actual behavior
We receive
ArgumentException
originating fromEnum.Parse
.Regression?
Nope, has been the case since NetFX but it is inconsistent and undocumented.
Impact
Unexpected exception for
TypeConverter
is thrown.Next Steps
I plan to submit a PR fixing this behavior, simply by using
TryParse
instead for the fallback.The text was updated successfully, but these errors were encountered: