Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Winform Application.
I defined a Event:
C#
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");
        }


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?
Posted
Comments
[no name] 28-Mar-11 8:30am    
Your question is not clear. Where are you trying to catch the exception?

Do this:

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


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):

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


In fact, your *should* define your own MyEventArgs class (must inherit from EventArgs) so that you can pass parameters to the event handler.
 
Share this answer
 
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.
 
Share this answer
 
If you want to catch every exceptions in your program, you can do something like that:

C#
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
    }
}
 
Share this answer
 
Comments
jason_mf 28-Mar-11 9:15am    
public partial class Form1 : Form
{
public delegate void TestEventHandler(object sender, EventArgs e);
public event TestEventHandler OnTestEvent;
public Form1()
{

InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
OnTestEvent += new TestEventHandler(Form1_OnTestEvent);
}

void Form1_OnTestEvent(object sender, EventArgs e)
{
throw new NotImplementedException();
}

private void button1_Click(object sender, EventArgs e)
{
//try
//{
if (OnTestEvent != null)
{
OnTestEvent(this, EventArgs.Empty);
}
//}
//catch (Exception ex)
//{
// MessageBox.Show("Publish Event:"+ex.Message);
//}
}

I write like this, and write "Application.ThreadException" and "AppDomain.CurrentDomain.UnhandledException" like you, but did not catch exception
Olivier Levrey 28-Mar-11 9:40am    
If you are in debug mode, the debugger will break first and then call your exception handler. Try your code in release.

And also make sure you have this line: Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
jason_mf 28-Mar-11 10:12am    
Now it's ok, Thanks very much.
Olivier Levrey 28-Mar-11 10:15am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900