0

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.

RockOnGom
  • 3,893
  • 6
  • 35
  • 53
  • The correct approach is to disable the just-in-time debugger setting that causes that dialog box to appear. Expecting applications to always exit cleanly, even in the face of unhandled exceptions, is unrealistic. For starters, there are some exceptions and internal errors that don't trigger any managed code at all. – Jeroen Mostert Aug 28 '17 at 10:45
  • 6
    Have you tried another exception type? I think a `StackOverflowException` *cannot* be caught. – Corak Aug 28 '17 at 10:45
  • 3
    If it's related only to `StackOverflowException`, check [this](https://stackoverflow.com/questions/1599219/c-sharp-catch-a-stack-overflow-exception) – vasek Aug 28 '17 at 10:45
  • 3
    From [MSDN](https://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx#Remarks) (it may be your case): Starting with the .NET Framework 4, this event is not raised for exceptions that corrupt the state of the process, such as stack overflows or access violations, unless the event handler is security-critical and has the HandleProcessCorruptedStateExceptionsAttribute attribute. – icebat Aug 28 '17 at 10:47
  • @vasek unfortunately it is not. I don't know what causes to crash my application, I am trying to find catch this unkown error and report it. I guess that exception could be an exception that can not be handled. That is why I am working on StackOverflowException. – RockOnGom Aug 28 '17 at 10:51
  • When an exception occurs the Net Library moves up the execution stack and finds the first exception handler. If you have method A-calls->B-calls->C. If you get an exception in C any exception handler in B will be called. So if A is main you will never capture the exception from C because the B exception handler will get the exception. – jdweng Aug 28 '17 at 10:54
  • Might be of interest: [How can I catch uncatchable exceptions?](https://stackoverflow.com/questions/3341534/partly-crashing-application-how-can-i-catch-uncatchable-exceptions) – Corak Aug 28 '17 at 10:54
  • @jdweng so it doesn't crash app. – RockOnGom Aug 28 '17 at 10:57
  • Microsoft to prevent blue screen create the manage Net Library. As part of the Net Library every application has a default exception handler that is added by the compiler/linker before main is called which will catch any exception that the code doesn't catch. All I'm saying is you exception handler isn't catching all unhandled exception because other exception handler are catching the exception. Some of you exception handlers may not be reporting any errors so you aren't seeing all the exceptions. – jdweng Aug 28 '17 at 11:04
  • face to same, i haven't found answer yet. – Tractatus Aug 28 '17 at 11:08
  • This has nothing to do with blue screens. Only kernel mode code can stop the system, blue screen. User mode code can't. Managed or unmanaged is just not relevant. Managed code is user mode, but so is an unmanaged desktop app, service, etc. – David Heffernan Aug 28 '17 at 12:17

0 Answers0