views:

420

answers:

5

I'm trying to gather a complete listing of the reasons a .NET process or thread to terminate, even though the main() method is guarded by a try...catch clause.

One such reason is Thread.Abort() (unless you call Thread.ResetAbort). Do you know of more reasons?

A: 

Maybe there is an unhandled exception occurring in your thread, ending in killing the thread; having a try catch clause in your main thread doesn't catch and exception from another thread running.

EDIT: Some concurrent access in read/write of a shared field

Stefano Driussi
A: 

Network connection timing out.

Power outage.

A user killing the process in question.

Robert
+4  A: 

StackOverflowException cannot be handled by your code.

A StackOverflowException typically occurs when you have an endless loop which lets your call-stack grow until the usual stack size (1MB) is exceeded.

There are more exceptions where you cannot recover from. ExecutionEngineException seems to be one of them.

0xA3
I recently had to fix a SOE caused by third party DLLs which consumed ~248K of IIS's 256K stack limit, just as an example of when it's not about infinite loops
annakata
Oh yeah, good to have examples where endless loops are not the cause. But I think the dll you used might do some recursive calls, otherwise I can't imagine how the stack limit could be exceeded so easily. Btw, I also experienced SOE when debugging recursice XSL templates. Recursion is evil!
0xA3
Passing huge objects by value as method parameters will also use up the available stack very quickly.
Stu Mackellar
+1  A: 

Unlike in C/C++, the main() is not quite the entirety of your application. So even surrounding all the code in main() with a try/catch block will not catch all exceptions generated by that code.

However, you can attach a function to handle unhandled exceptions thrown by the entire application by listening to the Application.ThreadException event, which will help you catch exceptions from any thread in the application, whether it was created by your code or not.

For example, your code may call upon the code in an external, unmanaged DLL. That code might execute threads of its own, which might asynchronously fail, causing an exception to be thrown. That exception belongs to the process that is your application, but not to any of your code. And if uncaught, will cause your program to terminate unexpectedly.

scraimer
A: 

Unloading the AppDomain

annakata