Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should converter throw any kind of exception?

Tags:

c#

wpf

Today i have come across a bug where the application starts throwing multiple exceptions(multiple messagebox). The Application uses DispatcherUnhandledException.

While investigating i've found that the converter used to format the date and time in the dataGrid throws the FormatException if the parameter to the converter is NULL.

Is it advisable to throw exception from the Converter in any case?

like image 941
Learner Avatar asked Aug 02 '12 16:08

Learner


Video Answer


1 Answers

According to MSDN, no:

The data binding engine does not catch exceptions that are thrown by a user-supplied converter. Any exception that is thrown by the Convert method, or any uncaught exceptions that are thrown by methods that the Convert method calls, are treated as run-time errors. Handle anticipated problems by returning DependencyProperty.UnsetValue.

That being said, personally I use the NotSupportedException when either the Convert or ConvertBack method is not supported.

like image 75
ken2k Avatar answered Nov 14 '22 02:11

ken2k