用户定义的事件有异常 [英] user-defined Event has a exception

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

问题描述

Winform应用程序.
我定义了一个事件:

Winform Application.
I defined a Event:

public delegate void TestEventHandler();
        public static event TestEventHandler OnTestEvent;

private void button1_Click(object sender, EventArgs e)
        {
            if (OnTestEvent != null)
            {
                OnTestEvent();
            }
        }

void Form1_OnTestEvent()
        {
           throw new NotImplementedException("12345");
        }



然后用UnhandledException捕获异常.
但是它没有捕获异常.

当我没有使用"try/catch"捕获异常时,该怎么办?



And I Catch the exception with UnhandledException.
But it did not Catch the Exception.

when I do not catch exception with "try/catch", how i to do?

推荐答案

执行以下操作:

Do this:

public delegate void TestEventHandler(); 
public static event TestEventHandler OnTestEvent = delegate{}; 



此时,您只需调用OnTestEvent(),而无需if块.

顺便说一句,您确实应该-至少要有以下参数(即使您不立即使用它们):



At that point, you an just call OnTestEvent() ewithout the if block.

By the way, you really should have - at the VERY least) the following parameters (even if you don''t use them right away):

public delegate void TestEventHandler(object sender, EventArgs e);



实际上,*应该*定义自己的MyEventArgs类(必须从EventArgs继承),以便可以将参数传递给事件处理程序.



In fact, your *should* define your own MyEventArgs class (must inherit from EventArgs) so that you can pass parameters to the event handler.


您似乎暗示您为
实现了处理程序 System.AppDomain.CurrentDomain.UnhandledException将捕获在非UI线程上发生的未处理异常.

使用System.Windows.Forms.Application.ThreadException捕获UI线程异常.

艾伦.
You seem to imply that you have implemented a handler for
System.AppDomain.CurrentDomain.UnhandledException which will trap unhandled exceptions occurring on non UI threads.

Use System.Windows.Forms.Application.ThreadException to trap UI thread exceptions.

Alan.


如果要捕获程序中的所有异常,可以执行以下操作:

If you want to catch every exceptions in your program, you can do something like that:

static class Program
{
    [STAThread]
    static void Main()
    {
        InitUnknownExceptionHandlers();
        ...
    }

    static void InitUnknownExceptionHandlers()
    {
        //exceptions thrown by the main thread (UI thread)
        Application.ThreadException +=
            new ThreadExceptionEventHandler(UIThreadUnhandledException);
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
        //exceptions thrown by other threads
        AppDomain.CurrentDomain.UnhandledException +=
            new UnhandledExceptionEventHandler(OtherThreadUnhandledException);
    }
    /// <summary>
    /// This function is called if an exception is thrown from the main thread (UI thread).
    /// </summary>
    static void UIThreadUnhandledException(object sender, ThreadExceptionEventArgs t)
    {
        //do something with the exception.
        MessageBox.Show("An unhandled exception occured:" + Environment.NewLine + t.Exception.Message);
        //you can also use ex.StackTrace to know where the exception was thrown
        //your program can still continue
    }
    /// <summary>
    /// This function is called if an exception is thrown from another thread.
    /// You can display a message to the user or do whatever you want here,
    /// but your application can't continue after: the framework will shut it down.
    /// </summary>
    static void OtherThreadUnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        Exception ex = (Exception)e.ExceptionObject;
        MessageBox.Show("An unhandled exception occured and the application will be terminated:" +
             Environment.NewLine + ex.Message);
        //you can also use ex.StackTrace to know where the exception was thrown
    }
}


这篇关于用户定义的事件有异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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