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 } }
更多推荐
用户定义的事件有异常
发布评论