当我创建一个新项目时,我对未处理的异常会有一个奇怪的行为.这就是我可以重现问题的方法:
1)创建一个新的Windows窗体应用程序(C#,. NET Framework 4,VS2010)
2)将以下代码添加到Form1_Load处理程序:
int vara = 5, varb = 0;
int varc = vara / varb;
int vard = 7;
Run Code Online (Sandbox Code Playgroud)
我希望VS中断并在第二行显示未处理的异常消息.但是,会发生的是第三行只是跳过而没有任何消息,应用程序一直在运行.
我现有的C#项目没有这个问题.所以我想我的新项目是用一些奇怪的默认设置创建的.
有没有人知道我的项目有什么问题?
我试过检查Debug-> Exceptions中的方框.但即使我在try-catch块中处理异常,执行也会中断; 这也不是我想要的.如果我没记错的话,在这个对话框中有一个名为"未处理的异常"的列或者类似的东西,这会让我感到非常兴奋.但在我的项目中只有一列("Thrown").
如何记录抛出和捕获的任何异常?像Visual Studio的IntelliTrace那样的东西.或者有没有办法将InteliTrace集成到应用程序的调试版本中,然后查看其日志?
更新:我会澄清一点.我想要标准的.txt(或任何自定义)日志,格式无关紧要.重点是我想记录所有第三方库中发生的所有异常,而不是向它们添加代码.
我创建了一个新的WPF应用程序,并为MainWindow中的Loaded事件添加了一个事件处理程序:
Loaded += (s, e) => { throw new Exception("AAAA!"); };
Run Code Online (Sandbox Code Playgroud)
然后我从Visual C#启动此应用程序,应用程序不会崩溃也不显示未捕获的异常.
我希望它会崩溃,这个应用程序确实在其他计算机上崩溃了.但为什么它适用于我的?
更新
添加了截图:
当我在事件处理程序中抛出异常时,异常处理程序没有被调用?
从以下开始的精简示例的示例代码:
应用程序.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>
Run Code Online (Sandbox Code Playgroud)
应用程序.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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
主窗口.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" …Run Code Online (Sandbox Code Playgroud)