Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未处理的ExceptionHandler Visual C未捕获在单独类中引发的异常(已移动)_C#_Winforms - Fatal编程技术网

C# 未处理的ExceptionHandler Visual C未捕获在单独类中引发的异常(已移动)

C# 未处理的ExceptionHandler Visual C未捕获在单独类中引发的异常(已移动),c#,winforms,C#,Winforms,为了使这个问题成为一个更简单、完整和可验证的例子,我从零开始,试图以最小的方式重现这个问题。请等待新问题链接 我已尝试实现一个全局无伤害异常处理程序 我有一个WFA Windows窗体应用程序 在Program类中,我实现了以下代码 static class Program { /// <summary> /// The main entry point for the application. /// </summary> [ST

为了使这个问题成为一个更简单、完整和可验证的例子,我从零开始,试图以最小的方式重现这个问题。请等待新问题链接

我已尝试实现一个全局无伤害异常处理程序

我有一个WFA Windows窗体应用程序

在Program类中,我实现了以下代码

    static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {   
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Utility.UnhandledExceptionOccurred);
        Application.ThreadException += new ThreadExceptionEventHandler(Utility.UnhandledExceptionOccurred);
        if (!EventLog.SourceExists("TestEventSource"))
        {
            EventLog.CreateEventSource("TestEventSource", "TestEventLog");
            MessageBox.Show("EventLog and Source Created. Restart Please");
            return;
        }
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }


}
        public void FormLoaded(object sender, EventArgs e)
    {
        var F = new Foo();
    }
当我在主UI线程中引发异常时,此代码工作正常,捕获异常,写入系统事件日志,然后退出

但是,假设我在同一名称空间中有一个新类

    class Foo
{
    public Foo()
    {
        throw new Exception("A");
    }

}
然后在Form1_onLoad中执行以下代码

    static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {   
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Utility.UnhandledExceptionOccurred);
        Application.ThreadException += new ThreadExceptionEventHandler(Utility.UnhandledExceptionOccurred);
        if (!EventLog.SourceExists("TestEventSource"))
        {
            EventLog.CreateEventSource("TestEventSource", "TestEventLog");
            MessageBox.Show("EventLog and Source Created. Restart Please");
            return;
        }
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }


}
        public void FormLoaded(object sender, EventArgs e)
    {
        var F = new Foo();
    }
引发了异常,但从未捕获

据我所知,没有第二个线程在后台运行,我必须为其实现新的未处理异常处理程序。有吗

这是怎么回事?如果我的类都在同一个名称空间中,为什么这些未处理的异常不会被应用程序中的句柄捕获

更多信息来源

    {

    public EventLog Log = new EventLog();
    Person P;
    public Form1()
    {
        InitializeComponent();
        Log.Source = "TestEventSource";
        Log.WriteEntry("Form Initialized and Running");
        throw new Exception("Exeption here is Caught");
    }

    public void FormLoaded(object sender, EventArgs e)
    {
        var F = new Foo(); //Exceptions here are not caught
    }
}

不,在FormLoaded中创建新的Foo的同时,我不会在Form1的构造函数中引发异常。我只是想澄清一下。不是一个就是另一个。当我在Form1构造函数中引发异常时,它被捕获。当我没有在Form1构造函数中引发所述异常,并在Form1Load中创建新的Foo时,该异常不会被捕获

我复制了你的大部分代码,用一个调试器替换了你的异常处理代码。中断并跳过事件日志,然后运行它。在OnLoad中创建Foo调用带有ThreadExceptionEventArgs的处理程序,在forms构造函数中创建Foo调用带有unhandledeexceptioneventargs的处理程序

我创建了一个小测试项目并运行了您的代码,一切都按照预期工作VS2015社区,AnyCpu/x64,.NET 4.0

编辑1: 现在我想我明白了。。。您的问题是,如果以Form.OnLoad运行异常代码,则Visual Studio会为您“中断”并显示未处理的异常窗口。。。那很好。按F5继续,将调用异常处理程序

据我所知,您可以在调试->窗口->异常设置下更改此行为

编辑2:
或者可能是这个

在多个类中有很多代码-请尝试将其简化为我们可以轻松复制/粘贴/编译/运行的代码-我不希望仅为了演示这一点就超过30行。对此表示抱歉。吸取的教训。@JonSkeet我能够重现并隔离问题。你为什么要提出第二个问题?你应该编辑这个问题。迈克尔,我已经能够完善我的问题,也许你会有兴趣看看我能推断出什么?