Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyConverter does not throw NotSupportedException on undefined values #9713

Open
h3xds1nz opened this issue Sep 4, 2024 · 2 comments
Open

Comments

@h3xds1nz
Copy link
Contributor

h3xds1nz commented Sep 4, 2024

Description

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):

object key = GetKey(fullName, CultureInfo.InvariantCulture);
if (key != null)
{
return ((Key)key);
}
else
{
throw new NotSupportedException(SR.Format(SR.Unsupported_Key, fullName));
}

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 with SR.Unsupported_Key message.

Actual behavior

We receive ArgumentException originating from Enum.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.

@miloush
Copy link
Contributor

miloush commented Sep 5, 2024

I tried to find some code that would break but didn't have much luck so far. Most people don't catch exceptions here, a few catch all exceptions.

@hongruiyu
Copy link

I tested it in WPF with the following code, and indeed an incorrect error occurred.

public void Fun()
{
    KeyConverter keyConverter = new KeyConverter();
    ITypeDescriptorContext context = null; 
    CultureInfo culture = CultureInfo.InvariantCulture;
    string keyString = "Ente"; //Incorrect keyString
    //string keyString = "Enter";   //Correct keyString

    try
    {
        Key key = (Key)keyConverter.ConvertFrom(context, culture, keyString);
        MessageBox.Show($"Converted key: {key}");
    }
    catch (NotSupportedException ex)
    {
        MessageBox.Show($"NotSupportedException: {ex.Message}");
    }
    catch (ArgumentException ex)
    {
        MessageBox.Show($"ArgumentException: {ex.Message}");
    }
    catch (Exception ex) 
    {
        MessageBox.Show($"Other Errors: {ex.Message}");
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants