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.