如果转换器抛出任何类型的异常? [英] Should converter throw any kind of exception?
问题描述
今天,我所遇到的,其中应用程序启动时抛出多个异常(多消息框)中的错误。该应用程序使用DispatcherUnhandledException。
Today i have come across a bug where the application starts throwing multiple exceptions(multiple messagebox). The Application uses DispatcherUnhandledException.
在调查我发现,用于格式化DataGrid中的日期和时间转换器抛出FormatException如果参数转换为NULL
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?
推荐答案
根据MSDN 无:
数据绑定引擎没有赶上由
用户提供的转换器抛出的异常。由转换
方法抛出的任何异常,或由该
转换方法调用,被视为运行时错误方法抛出任何未捕获的异常。通过返回DependencyProperty.UnsetValue处理
预期的问题。
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.
话虽这么说,我个人使用引发NotSupportedException
时,无论是转换
或 ConvertBack
不支持的方法。
That being said, personally I use the NotSupportedException
when either the Convert
or ConvertBack
method is not supported.
这篇关于如果转换器抛出任何类型的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!