Windows form_Load异常不会抛给父级 [英] Windows form_Load Exception is not throwing to parent

查看:95
本文介绍了Windows form_Load异常不会抛给父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





这是c#windows应用程序,它从父表单调用弹出

并且frmPopup_Load中有一个错误,它捕获异常,但没有抛到frmparnt形式



表单父
=======================================

Hi,

This is c# windows applicaiton, and it calling a popup from parent form .
And there is a error in the frmPopup_Load, its catching the exception, but not throwing to the frmparnt form

Form parent
=======================================

public frmParent()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                frmPopup frm = new frmPopup();
                frmPopup.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }







弹出代码

================================




Popup Code
================================

public frmPopup()
        {
            InitializeComponent();
        }

        private void frmPopup_Load(object sender, EventArgs e)
        {
            try
            {
                int i = 0;

                int j = 1 / i;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

推荐答案

通过将frmPopup_load访问修饰符私有更改为public来检查一次..
Check once by changing frmPopup_load access modifier private to public..


你可以创建一个

You could create a
public event Action<string> ExceptionMessageEvent;</string>



设置表格,当你发现异常时:

in the popup form.
Set in the form that when you catch the exception:

if (ExceptionMessageEvent != null)
    ExceptionMessageEvent(ex.Message);





在加载弹出窗口之前的父窗体中,父窗体可以在弹出窗体中订阅该ExceptionMessageEvent。

如果该事件是解雇你可以显示消息。



In the parent form just before loading the popup the parent form can subscribe to that ExceptionMessageEvent in the popup form.
If that event is fired you can show the message.


在64位系统上吞下表格的加载或显示事件中的异常(参见例如https://connect.microsoft.com/VisualStudio/feedback/details/357311 [ ^ ])。

因此在事件处理程序中处理异常,并在必要时关闭表单。
Exceptions in the Load or Shown event of a Form get swallowed on 64bit systems (see e.g. https://connect.microsoft.com/VisualStudio/feedback/details/357311[^]).
Hence handle the exception inside the event handler, and if necessary, close the form.


这篇关于Windows form_Load异常不会抛给父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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