-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Issue3888 fix #4016
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
Issue3888 fix #4016
Conversation
| //null value will be used in design mode | ||
| if (item == null) return null; | ||
|
|
||
| var num = Convert.ToInt32((string)item); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can still produce FormatException.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The try catch should be there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to example description there is no any values in this example that can produce FormatException and this TemplateSelector does not have meaning in working with other data types and data formats.
If you are talking about future changes to this code, note that in fact this can also produce OutOfMemoryException, InvalidCastException, ThreadAbortException and any type of exception in this lines as well as in any other lines. Catching any of exceptions we do not expect will lead to an issue, silently caught and masked by the behavior implemented inside catch block. For example: catch(FormatException){return null;} will produce the behavior not stated in example description and behavior neither user of application nor user (developer) of this class expect.
Statement from c# Programming Guide:
Do not catch an exception unless you can handle it and leave the application in a known state. If you catch System.Exception, rethrow it using the throw keyword at the end of the catch block.
More information here: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/exceptions/
So, allowing this code to throw FormatException actually allows user if this type to be notified about wrong usage (as Exceptions in .NET introduced for).
If we want to make this situation better we can write additional code, like:
try
{
var num = Convert.ToInt32((string)item);
}
catch(InvalidCastException invalidCastException)
{
throw new InvalidCastException($"Every item passed to this template must be of type {typeof(String)}",invalidCastException);
}
catch(FormatException formatException)
{
throw new InvalidCastException($"Every item string passed to this template must be convertible to {typeof(int)}",invalidCastException);
}
This is only a small part of possible exceptional situations, but it shows several times more code than necessary for TemplateSelector explainations.
I see the possibility to create generic TemplateSelector where T must be if type int. In this case it is possible to avoid errors in runtime and to get them in compile time, but again, there will be too much of code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If thats the desired behavior i'm ok with it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, with what exactly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The exception handling.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyway, FormatException is not possible in this example, don't you agree?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I agree.
| } | ||
|
|
||
| //<Snippet2> | ||
| public class NumderDataTemplateSelector : DataTemplateSelector |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there's a typo in the class name: it's "Numder" when it should be "Number". Since you're already refactoring the class, do you want to fix that too? (And when doing that, don't forget to also change the name in XAML.)
|
Thanks so much @jinek! The changes look good. Since we have a VB pair for this snippet, that would also have to be changed. Do you wanna make that change or should I? Thanks! |
mairaw
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's great @jinek. Thanks for making the changes to both VB and C# samples. Your fix should go live with our next update cycle, probably in the next day or so.
|
@mairaw You are welcome! |
Issue3888 fix
Summary
Improper exceptions behavior removed. Unnecessary "as operator" removed. Refactoring.
Fixes #3888
Details
More information in top 5 comments here #3888
Suggested Reviewers
@mairaw