How to catch unhandled exceptions in C#

1.8k views Asked by At

I have a service manager that checks and controls services that are connected to it. But sometimes one of my service is getting crushed and service manager can't know whether it is still working or not. (Because of the dialog box which shows debug and close options). So i found an accepted solution that catches unhandled exceptions, but doesn't work in my code. Here is my code that doesn't catch unhandled exceptions.

static void Main(string[] args) 
{
  AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(unhandled_exceptions);
  new Thread(() => 
  {
      start()
  }).Start();

}

public void unhandled_exceptions(object sender, UnhandledExceptionEventArgs e) 
{
  this.disconnect();
  logger.Error(e);
  logger.Info("Catched unhandled exception, that causes to crash application. Disconecting from service manager..");
  Environment.Exit(1);
}

public override void start() 
{
  try 
  {
    throw new ArgumentException("erer");
  } 
  catch (Exception ex) 
  {
    logger.Error(ex);
  }

  Thread.Sleep(5000);
  PerformOverflow();

}

void PerformOverflow() 
{
  PerformOverflow();
}

.Net 4.6, Console applicaiton

I don't know what causes to crash my application, in actual fact the start method is in a try catch blog. I am using StackOverflowException because it is not unhandled too. I think If i catch it i could catch the real one that crashes my app.

0

There are 0 answers