Click here to Skip to main content
15,915,042 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I am having an issue while trying to catch a recursive exception, hope someone can help.
Here is my Program.cs code configured to handle exceptions.
static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            Application.ThreadException += Application_ThreadException;

            Application.Run(new Form1());
        }

Here is the use case.
1. User will load the Form1 and there i am expecting some exception might come due to data load errors.
private void Form1_Load(object sender, EventArgs e)
{throw new Exception("Data Load Error");}


2. Then It will hit
Application_ThreadException;
event and there we are trying to Close the form and Reload it Once again.

 static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
             Form currentForm = GetActiveMainForm();
            if (currentForm != null)
            {
string formToOpen = currentForm.Name;
currentForm.BeginInvoke(new MethodInvoker(currentForm.Close));
                object form = Assembly.GetEntryAssembly().CreateInstance("ExceptionHandling." + formToOpen);
                (form as Form).Show();
}

}

So far it works fine. Now
3.When i ReLoad the Form, it should throw the same exception and
Application_ThreadException
should fire and this process supposed to be
happen recursively.
First Load=>Error=>CloseAndReLoad=>Error=>CloseAndReload=>Error.. so on..
HERE , I am trying to check if data load fails 3 times , then close the Module/Form

But when it Loads the Form 2nd time(after step 2) and throws the exception, it is not going to
Application_ThreadException
event handler.

I am trying to figure out the root cause for this behaviour, but couldn't.

Hope somebody will shed some light here, if am missing anything or is this the default expected behavior?

Thanks in advance,
Prathap

What I have tried:

I have already tried setting
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);

which handles to throw the exception(using AppDomain.CurrentDomain.UnhandledException event) recursively, but even after handling exception, it wont exit and keep on throwing the same exception.
Posted
Updated 5-Mar-17 7:52am

1 solution

 
Share this answer
 
Comments
Prathap S V 5-Mar-17 14:45pm    
That didn't help, ThreadException event is not firing second time same as above.
Graeme_Grant 5-Mar-17 14:49pm    
did you try their sample code in a new project?
Prathap S V 6-Mar-17 1:19am    
yes, that sample code as it is, will work. But i have modified to suit my requirement,i.e, I am expecting an exception at Load Event and trying Re-Load that form for 3 times through Application_ThreadException event. Even after 3 times if am still getting the exception in Form Load then i want to close that form, Which is not working still.
Graeme_Grant 6-Mar-17 1:27am    
I'm neck deep in complex Xml programming atm, so I don't have time to bake a mock from scratch. Post a link to a downloadable sample (remove bin & obj folders) and I'll fire it up and see if I can spot where you are going wrong quickly.
Prathap S V 6-Mar-17 5:27am    
I have shared a sample, you can download here.
http://wikisend.com/download/617932/ExcHandling.rar

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