未处理的异常下一行或退出 [英] Unhandled Exception next line or exit

查看:35
本文介绍了未处理的异常下一行或退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[更新 2:问题已解决,请参阅下面和我的其他帖子中的评论.希望这对某人有所帮助.]

[update 2: Problem solved, see my comment below and in my other post. Hope this helps someone.]

[更新:对我的另一个问题的回答在 Beta 测试期间检测异常,作为这个问题的基础是有缺陷的.在 Visual Studio 中,2010 和现在的 2012 运行良好.异常处理程序被调用,好的,在 VS 在线中断后,我说继续.我决定在 VS2012 IDE 之外进行测试,这是好事.操作系统捕获错误,显示标准的发生未处理的异常"对话框,提供详细信息以及继续和退出按钮.选择继续,只是继续应用程序,而不会陷入我的超级异常处理程序.选择完全关闭应用程序并显示标准的关闭窗口对话框.退出按钮也不会调用我的超级处理程序.

[update: The response to my other question Detecting Exceptions During Beta Testing, which served as the basis of this question is flawed. Inside the Visual Studio both 2010 and now 2012 works great. The exception handler gets called, okay after VS breaks at the line and I say to continue. I decided to test outside the VS2012 IDE, good thing for that. The OS traps the bug, shows the standard an "An Unhandled Exception Occurred" dialog giving the details along with a continue and quit buttons. Selecting continue, just continues the application with no trapping into my uber exception handler. Selecting quite, whites-out the application and displays the standard close window dialog. The quit button also does not call my uber handler.

目的是为了调用我的异常处理程序.如果我在 VS2012 IDE 中工作,我不需要 uber 异常处理程序.处理程序的目的是面向最终用户和 Beta 测试人员,即除我之外的任何人,并且不会拥有我的开发站.

The purpose is so that my exception handler gets called. I do not need an uber exception handler if I am working inside the VS2012 IDE. The purpose of the handler is for end users and beta testers, namely anyone other than myself and who will not have my development station.

下面的代码在 IDE 之外不起作用.因此,关闭应用程序和继续这两个按钮毫无意义,因为异常处理程序永远不会被调用.在 IDE 下运行并玩代码时(为什么这不是在浪费我的时间?),continue 不会继续,exit 也不会退出.我只是一遍又一遍地看到相同的异常.是的,我正在尝试发布的可能答案.

The code below does not work outside the IDE. As such, the two buttons Close App and Continue are meaningless, as the exception handler never gets called. While running under the IDE and playing with the code (why is that not an abject waste of my time?), the continue does not continue and the exit does not exit. I just see the same exception over and over again. Yes, I am tring the posted possible answer.

我真的认为这个话题会被 .Net 4.5 很好地解决和解决.]

I really would think that this topic would be well worn and solved with .Net 4.5. ]

我添加了一个未处理的异常处理程序,它与我的主窗体无关,我在 Application.Run(new frmMain()) 调用之前添加了它.处理程序按预期工作,没有问题.

I added an unhandled exception handler, which is indepedent of my main form, which I added just prior to the Application.Run(new frmMain()) call. The handler works as expected, no issues.

问题:1. 是否可以强制关闭/结束违规表单(基本上是关闭应用程序)?

Questions: 1. Is it possible, and if so how, to forcefully close / end the offending form (close app, basically)?

我在主窗体上添加了一个按钮,在 OnClick 事件中,我只是简单地除以零,以便集中模拟一些不好的东西.我在 VS2012 调试环境下运行.自然地,我首先遇到了 VS 提醒我的违规行,但是在继续执行时,我遇到了未处理的异常处理程序.在该处理程序中,一种选择是结束执行.我执行了Application.Exit()",但没有效果.我一直回到那条有问题的路线上.

I added a button on the main form and in the OnClick event, I simply divided by zero, so as to focefully simmulate something bad. I am running with the VS2012 debugging environment. Naturally, I hit first the offending line, which VS allerted me to, however upon continuing execution, I hit my unhandled exception handler. Inside that handler, one option is to end execution. I executed "Application.Exit()" but that had no effect. I kept going right back to that offending line.

  1. 是否可以通过异常处理程序以编程方式从下一行继续执行?

简单地返回只会让我回到那个点.

Simply returning just gets me right back to that point.

我已经知道 try/catch 块,但这里的目的是捕获我不期望的未处理异常,并且在最坏的情况下,可以向我发送从该处理程序生成的有意义的崩溃报告.

I already know of try/catch blocks, but the purpose here is to catch unhandled exceptions that I do not expect and that in worst case, I can be sent a meaningful crash report that I generate from that handler.

JMK 要求提供一些示例代码,但不确定这会带来什么.

JMK asked for some sample code, but not really sure what that would entail.

我抛出一个对话框以响应未处理的异常.有 4 个按钮,其中两个用于本次发布.第一个按钮允许用户继续,而第二个按钮终止应用程序.至少,这是一个想法.

I throw up a dialog in response to an unhandled exception. There are 4 buttons, two for the purpose of this posting. The first button allows the user to continue, while the second button terminates the application. At least, that is the thought.

        private void cmdContinue_Click(object sender, EventArgs e)
    {
        // Close this dialog and attempt to resume.
        this.DialogResult = DialogResult.OK;
        this.Close();
    }

    private void cmdExitApp_Click(object sender, EventArgs e)
    {
        // Close this dialog and attempt to resume.
        this.DialogResult = DialogResult.OK;
        this.Close();

        // Exit the application.
        Application.Exit();
    }

continue 块,至少在 VS2012 IDE 中,只是保持在违规行.退出块也是如此.

The continue block, at least from within VS2012 IDE, merely keeps at the offending line. Ditto for the exit block.

就我的例外而言,例外可以是任何事情.这就是我写它的原因.我的专用例外只是测试代码,然后按钮再见.我正在努力创建一个完美的不间断应用程序,但是这个目标是虚幻的,直到那时这个块.这种机制的另一个目的是让 Beta 测试人员向我报告错误.

As far as my exception goes, the exception can be anything. That is why I wrote it. My dedicated exception is merely to test out the code and then the button goes bye-bye. I am trying to be diligent and create a perfect non-breaking application, however that goal is illusive and until that time this block. The other purpose of this mechanism is for beta testers to report bugs to me.

这是我的超快速和肮脏的未处理异常的代码.我必须很棘手,因为简单地除以零会被编译器捕获.顺便说一句,未处理的异常通常不会发生在我编写的特定行上,而是发生在方法内部调用其他人的代码中.

Here is the code for my ultra quick and dirty unhandled exception. I had to be tricky, because simply dividing by zero would get caught by the compiler. By the way, unhandled exceptions usually occur not on a specific line that I write, but in calls to someone else's code, inside a method.

        private void button1_Click(object sender, EventArgs e)
    {
        // Attempt to throw and unhandled exception to test out the unhandled exception handler.
        int iDivider = 0;
        int iResult = 5 / iDivider;
    }

这是另一个代码块,即测试应用程序的启动.

Here is another code block, namely the starting of the test application.

        public static frmMain FormMain = null;

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        // 
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        // Turn on global unhandled exception handling.
        cExceptions.Initialize();

        // Run the application load tasks manually, as Windows will not call this event until the show.
        Program.FormMain = new frmMain();

        Application.Run(Program.FormMain);
    }

有人要我的异常处理程序,所以它来了.基本上,微软给了样本.

Someone asked for my exception handler, so here it is. Basically, Microsoft gave the sample.

    /// <summary>
/// Based on example at http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx
/// </summary>
internal static class cExceptions
{
    [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
    public static void Initialize()
    {
        AppDomain currentDomain = AppDomain.CurrentDomain;
        currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
    }

    static void MyHandler(object sender, UnhandledExceptionEventArgs args)
    {
        Exception ex = (Exception)args.ExceptionObject;


        // Log the exception to disk.

        // Show the exception information to the user.
        using (frmExceptions oException = new frmExceptions())
        {
            // Set the exeption information.
            oException.oEx = ex;

            // Show the dialog.
            oException.ShowDialog();
        }

        Console.WriteLine("MyHandler caught : " + ex.Message);
    }
}

提前致谢.

推荐答案

好吧,Google Uberlord 对我很不满,我找到了这篇解决问题的精彩文章.赞美全能的谷歌.

Okay, the Google Uberlord took pitty upon me and I found this wonderful article solving the problem. Praise to the almighty Google.

链接如下:http://www.switchonthecode.com/tutorials/csharp-教程处理未处理的异常

基本上,JRadness 的想法是正确的,只是略有错误.他应该用

Basically, JRadness had the right idea, just slightly in error. He should have used

Application.ThreadException += 
    new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

而不是

AppDomain.CurrentDomain.UnhandledException += 
    new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

Switch on the Code的作者讲了三种方法,第一种是JRadness提出的,对Windows窗体不起作用.

The author on Switch on the Code talked of three methods, the first of which is what JRadness proposed and does not work for Windows forms.

作者甚至解决了我的另一个继续和中止问题.操作系统被绕过.这是他的事件处理程序.

The autor even solved my other question of Continue and Abort. The OS got bypassed. Here is his event handler.

public static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
    DialogResult result = DialogResult.Abort;
    try
    {
        Exception ex = (Exception)e.Exception;
        MessageBox.Show("Whoops! Please contact the developers with the"
             + " following information:\n\n" + e.Exception.Message + e.Exception.StackTrace,
             "Application Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
    }
    finally
    {
        if (result == DialogResult.Abort)
        {
            Application.Exit();
        }
    }
}

是的!!

这篇关于未处理的异常下一行或退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆