开发者

Asynchronous .NET Exceptions

开发者 https://www.devze.com 2023-03-23 19:28 出处:网络
I have got a problem with a .NET application. It terminates without throwing any exception. If I start it in debug mode in my visual s开发者_如何学编程tudio, the debugging terminates also without any

I have got a problem with a .NET application. It terminates without throwing any exception. If I start it in debug mode in my visual s开发者_如何学编程tudio, the debugging terminates also without any error message.

I have found the post StackOverflowException in .NET which describes that this behaviour occurs when the CLR throws one the following exceptions:

- ThreadAbortException

- OutOfMemoryException

- StackOverflowException

But how can I determine which of these exception was thrown in my case?

Is it possible to log the exception and maybe to get a stack trace or the type of the exeption at least?

I have tried to write a second application, which starts the other app in a separate process. With this approach, I could detect when the process terminated, but the only information I could get in this way was the exit code -532459699. Does anyone know what that means?


Try to set Visual Studio -> Debug -> Exceptions -> CLR Exceptions


Did you configure Visual Studio debugger to catch all thrown exceptions? If not go to Debug/Exceptions... and check the box under the thrown column next to Common Language Runtime Exceptions


Handle the domain exceptions at the main on the program:

AppDomain.CurrentDomain.UnhandledException += OnCurrentDomain_UnhandledException;

//Add these too if you are in windows forms application
Application.ThreadException += OnApplication_ThreadException;

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

Then if exception thrown just log it, and you can also restart your program from here without needing for another application to watch your process.

private static void OnCurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
#if DEBUG
    System.Diagnostics.Debugger.Break();//we will break here if we are in debug mode.
#endif//DEBUG

    LogException(e);
    RestartTheApplication();    
}
0

精彩评论

暂无评论...
验证码 换一张
取 消