用户定义的事件有异常

编程入门 行业动态 更新时间:2024-10-12 12:24:06
本文介绍了用户定义的事件有异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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 } }

更多推荐

用户定义的事件有异常

本文发布于:2023-05-28 06:59:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/315185.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:词库加载错误:Could not find file &#039;D:\淘小白 高铁采集器win10\Configuration\Dict_Sto

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!