当我在事件处理程序中抛出异常时,异常处理程序不会被调用?
以下是一个简化示例的示例代码:
我已经查看了这个问题和这个问题,但是强制使用32位或64位甚至"ANY CPU"都不起作用。另外,当设置这四个(!)处理程序中的任何一个时,在事件抛出异常时没有任何一个处理程序被调用。此外,这篇文章也没有帮助。
我正在运行VS2012(在Win8,x64上),该项目使用.Net Framework 4.5)。我错过了什么?我疯了吗?
为了更清晰:我期望会显示一个消息框(当我点击
以下是一个简化示例的示例代码:
App.xaml
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
DispatcherUnhandledException="App_DispatcherUnhandledException" >
<Application.Resources/>
</Application>
App.xaml.cs
using System.Windows;
using System.Windows.Threading;
namespace WpfApplication1
{
public partial class App : Application
{
//This method is called when ButtonA is clicked, but not when ButtonB is
//clicked (and a (random) file is selected ofcourse).
void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show(e.Exception.Message, "An exception occurred", MessageBoxButton.OK, MessageBoxImage.Error);
e.Handled = true;
}
}
}
MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Button A" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="ButtonA_Click"/>
<Button Content="Button B" HorizontalAlignment="Left" Margin="90,10,0,0" VerticalAlignment="Top" Width="75" Click="ButtonB_Click"/>
</Grid>
</Window>
MainWindow.xaml.cs
using Microsoft.Win32;
using System;
using System.Windows;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonA_Click(object sender, RoutedEventArgs e)
{
throw new Exception("Works!");
}
private void ButtonB_Click(object sender, RoutedEventArgs e)
{
var ofd = new OpenFileDialog();
ofd.FileOk += (s, ce) => {
throw new Exception("Does not work!?!?");
};
ofd.ShowDialog();
}
}
}
我已经查看了这个问题和这个问题,但是强制使用32位或64位甚至"ANY CPU"都不起作用。另外,当设置这四个(!)处理程序中的任何一个时,在事件抛出异常时没有任何一个处理程序被调用。此外,这篇文章也没有帮助。
我正在运行VS2012(在Win8,x64上),该项目使用.Net Framework 4.5)。我错过了什么?我疯了吗?
为了更清晰:我期望会显示一个消息框(当我点击
ButtonA
时它确实显示了),或者,实际上,App_DispatcherUnhandledException
方法被调用。但是当我点击ButtonB
时,该方法没有被调用(因此)消息框也没显示。 ButtonA
和ButtonB
之间的唯一区别是"A
"中的异常不在事件处理程序中,而"B
"中的异常在事件处理程序中。当然,我确保在OpenFileDialog
中选择了文件并单击“打开”以选择它。调试器启动并指出抛出了"Does not work!?!?"异常,然后我继续执行,但没有显示任何消息框。
另外:我对WPF非常新,这可能是问题的一部分
编辑1
供参考,以下是演示确切问题的两个zip文件:
在我的计算机上,对于上述两个项目,ButtonA会导致消息框被显示出来,而ButtonB(选择文件后)则不会。即使打开“调试非托管代码”,也是如此。
编辑2
因此,我在另一台计算机上运行了相同的代码,发现以下情况:在另一台计算机上,调试器会显示:
请注意标题中的Exception crossed a native/managed boundary
。当我尝试恢复执行(继续)时,异常一直弹出。当调试器启动时,我的机器显示:
当我恢复执行时,异常会消失在某种黑洞中;主窗体再次显示,什么都没有发生。
这可能与以下设置有关:
然而,即使打开/关闭此选项也没有帮助,即使重新启动VS2012并删除临时文件(以及从项目中的bin / obj目录中),恢复默认值等。
所以...我现在知道异常确实与托管和非托管之间的跨界有关。现在我只需要找出如何解决这个问题,以便我可以在FileOk
事件中抛出异常(以便最终,我的组件也可以在那里抛出异常)。
FileOK
(!))事件处理程序中抛出了一个异常。该异常在事件处理程序(MainWindow.xaml.cs
)的“第一行”中抛出。(FileOK
(!))事件处理程序中没有其他代码。接下来,我希望调用App_DispatcherUnhandledException
事件处理程序,因为这是一个未处理的异常。为了演示目的,我只是显示一个消息框,但当然最终还需要做更多的工作(如日志记录等)。问题是App_DispatcherUnhandledException
从未被调用,就像它应该被调用一样(我认为)。 - RobIII