I have been doing some reading lately about Exception Handling and the .Net Framework and this seems to have left me more confused then when I started.
In CLR via C# Fourth Addition I Read:
Also, the .NET Framework exception-handling mechanism is built using the Structured Exception Handling (SEH) mechanism offered by Mic开发者_JS百科rosoft Windows. SEH has been discussed in many resources, including my own book, Windows via C/C++, 5th ed. (Microsoft Press, 2007), which contains three chapters devoted to SEH.
Now, here is where my confusion starts. If the .Net Framework is built Using the SEH but is this only used for an unhandled exception or is it used right off the bat for all exception handling in .NET if so where does the CLR fit in, is taking Queues from SEH or is it acting on its own and using SEH if and when an unhandled exception Occurs?.
Second How does Context switching from the Kerrnal mode to User mode fit in? I know when a thread is created it has (From CLR via C#)
Thread environment block (TEB) The TEB is a block of memory allocated and initialized in user mode (address space that application code can quickly access). The TEB consumes 1 page of memory (4 KB on x86 and x64 CPUs, 8 KB on an IA64 CPU). The TEB contains the head of the thread’s exception-handling chain. Each try block that the thread enters inserts a node in the head of this chain; the node is removed from the chain when the thread exists in the try block. In addition, the TEB contains the thread’s thread-local storage data as well as some data structures for use by Graphics Device Interface (GDI) and OpenGL graphics.
AS well as the User Mode Stack and The Kernel mode stack. But Why would a context switch need to happen at any point as they have everything needed to process is on the same thread?
Here is a very detailed explanation about how the CLR handles exceptions (by one of the CLR architects I believe): http://blogs.msdn.com/b/cbrumme/archive/2003/10/01/51524.aspx
is [SEH] used right off the bat for all exception handling in .NET
Yes. Throwing a .NET exceptions causes a Win32 structured exception to be raised.
Why would a context switch need to happen at any point as they everything needed to process is on the same thread?
When you throw an exception, your thread transitions to kernel mode and back, but only one thread is involved. Kernel mode is not a separate thread; it's a place that your thread can visit.
All Win32 exceptions have to transition via the kernel. For instance, the debugger infrastructure lives in kernel mode, and a debugger might want to intercept the exception before your user mode code sees it.
精彩评论