【发布时间】:2011-01-21 06:16:40
【问题描述】:
在 Silverlight 4 应用程序中,我在包含的控件 (DataGrid) 上调用函数,该函数有时会引发 MS.Internal.WrappedException 类型的虚假异常。由于这个异常没有意义,我需要吞下它。不幸的是,异常在 System.Windows.dll 中声明为 internal class WrappedException : Exception,所以我不能在 catch 块中命名它。
问题是,检测此异常并忽略它的最安全方法是什么?我想出的两个选项是:
- 查找原始异常:
ex.InnerException is InvalidOperationException - 查找名称:
ex.GetType().FullName == "MS.Internal.WrappedException"
一种方式比另一种更好吗?还有其他我没有想到的选择吗?
这是显示不同选项的函数:
private void SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedAlarm = alarmList.SelectedItem as Alarm;
if (selectedAlarm != null)
{
dataGrid.SelectedItem = selectedAlarm.Source;
try
{
dataGrid.ScrollIntoView(dataGrid.SelectedItem, null);
}
// catch (MS.Internal.WrappedException ex) doesn't compile
catch (Exception ex)
{
if (ex.InnerException is InvalidOperationException) // 1
if (ex.GetType().FullName == "MS.Internal.WrappedException") // 2
{
// ignore exception
}
else
throw;
}
}
}
对于那些感兴趣的人,这里是 StackTrace:
在 MS.Internal.XcpImports.CheckHResult(UInt32 小时) 在 MS.Internal.XcpImports.UIElement_Measure(UIElement 元素,Size availableSize) 在 System.Windows.UIElement.Measure(可用大小) 在 System.Windows.Controls.DataGrid.InsertDisplayedElement(Int32 插槽,UIElement 元素,布尔 wasNewlyAdded,布尔 updateSlotInformation) 在 System.Windows.Controls.DataGrid.InsertDisplayedElement(Int32 插槽,布尔 updateSlotInformation) 在 System.Windows.Controls.DataGrid.GetExactSlotElementHeight(Int32 插槽) 在 System.Windows.Controls.DataGrid.ScrollSlotIntoView(Int32 插槽,布尔滚动水平) 在 System.Windows.Controls.DataGrid.ScrollSlotIntoView(Int32 columnIndex,Int32 插槽,布尔 forCurrentCellChange,布尔力水平滚动) 在 System.Windows.Controls.DataGrid.ScrollIntoView(对象项,DataGridColumn 列) 在 DtDemo.Home.alarmList_SelectionChanged(对象发送者,SelectionChangedEventArgs e)这里是 InnerException.StackTrace:
在 System.Windows.Controls.DataGridRow.get_ActualDetailsVisibility() 在 System.Windows.Controls.DataGridRow.OnApplyTemplate() 在 System.Windows.FrameworkElement.OnApplyTemplate(IntPtr nativeTarget)【问题讨论】:
标签: c# silverlight exception