尝试捕获无法在WinForms应用程序中正常工作 [英] try catch not working properly in WinForms Application

查看:80
本文介绍了尝试捕获无法在WinForms应用程序中正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在旧版Winforms应用程序中捕获异常,但总会以某种方式通过全局异常处理捕获异常。

I am trying to catch an exception in a legacy winforms applications but somehow the exception is always captured by the global exception handling.

在Program.cs中,以下代码:

In the Program.cs I have the following code :

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

Application.ThreadException += Application_ThreadException;
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
    //throw new NotImplementedException();
    MessageBox.Show(e.Exception.Message);
}

并具有以下代码形式:

private void gttBindingSourceLosplaatsen_PositionChanged(object sender, EventArgs e)
{
    try
    {
        bool enabled = ((DataTable)gttBindingSourceLosplaatsen.DataSource).Rows.Count > 0;
        buttonEditLosplaats.Enabled = enabled;
        buttonDeleteLosplaats.Enabled = enabled;
    }
    catch 
    {
        MessageBox.Show("what is this ?");
    }
}

现在,当 buttonDeleteLosplaats.Enable =启用行时; 被执行时发生异常。现在,我希望落入catch块并显示消息这是什么?。但这不会发生,我总是掉入全局异常处理程序中。

Now when the line "buttonDeleteLosplaats.Enable = enabled;" is executed an exception occurs. Now I expect to fall into the catch block and show the message "What is this ?" but that does not happen, I always fall into the global exception handler instead.

这可能是什么原因?此应用程序中还有许多其他try..catch构造可以按我的预期工作,并且确实属于catch块,但不是这个。
是否有人知道为什么我不属于本地try ... catch块,而是属于全局异常处理程序?

What could cause this ? There are a lot of other try..catch constructs in this application that do work as I expect and do fall into the catch block, but not this one. Does anybody has an idea why I do not fall into the local try...catch block but into the global exception handler ?

推荐答案

我通过将代码更改为此来解决了问题:

I solved my problem by changing my code to this :

gttTextBoxLotnr.Focus();
this.ActiveControl = gttTextBoxLotnr;

bool enabled = ((DataTable)gttBindingSourceLosplaatsen.DataSource).Rows.Count > 0;
buttonEditLosplaats.Enabled = enabled;
buttonDeleteLosplaats.Enabled = enabled;

现在不再出现索引-1没有值的异常,并且我没有不再需要尝试。
buttonDeleteLosplaats具有Focused属性= true,并且在将enabled属性设置为false时以某种方式导致了异常。

Now the exception "Index -1 does not have a value" does not occurs anymore and I have no need anymore for the try catch. buttonDeleteLosplaats had the Focused property = true and that somehow caused the exception when setting the enabled property to false.

这仍然给我留下了两个问题永远不会被回答:

That still leaves me with 2 questions wich will probably never be answered:


  1. 我仍然不知道为什么没有达到我的catch子句

  2. 设置对于焦点集中的控件,将enabled属性设置为false会引发异常,这是为什么呢?我想不出任何一种情况,如果这种情况严重到足以引发异常的情况,表单就应该没有任何主动控制,就是这样,没有危害,在任何宇宙中的任何地方都没有问题,那为什么要引发异常? li>
  1. I still do not know why my catch clause was not reached
  2. Setting the enabled property to false for a focused control throws an exception, why is that needed ? I cannot think of any condition where this is serious enough to throw an exception for, the form should just be left with no active control, that's it, no harm done, no problem anywhere in any universe, so why throw an exception ?

这篇关于尝试捕获无法在WinForms应用程序中正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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