MSDN Magazine - February 2009 - (Page 21) happened. For example, consider a program that calls ficult to write correct code that handles a CSE and con[HPCSE] native code that’s known to be buggy. Debugging into tinues running the process safely. fn1() { the code you learn that it sometimes zeros a pointer beLook closely at the code in Figure 4 to see what could try {} fore accessing it, which causes an access violation. You go wrong. If this code is not in a function that can procatch(){} } might want to use the HandleProcessCorruptedStatecess CSEs, then the finally block will not run when an Exceptions attribute on the function which calls native access violation happens. That’s fine if the process tercode using P/Invoke because you know the cause of the minates—the open file handle will be released. But if fn2() pointer corruption and are comfortable that the process some other code catches the access violation and tries integrity is maintained. to restore state, it needs to know that it has to close this The other scenario that may call for the use of this atfile as well as restore any other external state this profn3() { tribute is when you’re as far as you can be from the error. gram has changed. try {} nally {} In fact, you’re almost ready to exit your process. Say you’ve If you decide that you’re going to handle a CSE, your } written a host or a framework that wants to perform some code needs to expect that there is a ton of critical state custom logging in case of an error. You might wrap your that has not been unwound. Finally and fault blocks have main function with a try/catch/finally block and mark fn4() { not been run. Constrained execution regions have not it with HandleProcessCorruptedStateExceptions. If an been executed. The program—and the process—are in Access Violation! error unexpectedly makes it all the way up to your pro- } an unknown state. gram’s main function, you write some data to your log, If you know your code will do the right thing, then doing as little work as you have to, and exit the process. Figure 3 Excepyou know what to do. But if you’re not sure of the state When the integrity of the process is in question any work tion and Access your program is executing in, then it’s better to just let you do could be dangerous, but if the custom logging fails Violation your process exit. Or, if your application is hosted, insometimes it’s acceptable. voke the escalation policy your host Take a look at the diagram shown Figure 4 The Finally Block May Not Run has specified. See Alessandro Catorcini in Figure 3. Here function 1 (fn1()) void ReadFile(int index) and Brian Grunkemeyer’s CLR Inside { is attributed with [HandleProcessOut column from December 2007 at System.IO.StreamReader file = CorruptedStateExceptions] so its catch msdn.microsoft.com/magazine/cc163298 for new System.IO.StreamReader(filepath); try clause catches the access violation. The more information on writing reliable { finally block in function 3 does not excode and CERs. file.ReadBlock(buffer, index, buffer.Length); } ecute even though the exception will catch (System.IO.IOException e) be caught in function 1. Function 4 at Code Wisely { Console.WriteLine("File Read Error!"); the bottom of the stack raises an acEven though the CLR prevents you } cess violation. from naively catching CSEs, it’s still finally { There is no guarantee in either of not a good idea to catch overly broad if (file != null) these scenarios that what you’re doclasses of exceptions. But catch (Excep{ file.Close() ing is completely safe, but there are tion e) appears in a lot of code, and it’s } scenarios where just terminating the unlikely that this will change. By not } } delivering exceptions that represent process is unacceptable. However, if you decide to handle a CSE there is a a corrupted process state to code that huge burden on you as the programmer to do it correctly. Remem- naively catches all exceptions, you prevent this code from making ber that the CLR exception system won’t even deliver a CSE to any a serious situation worse. function that is not marked with the new attribute either during The next time you write or maintain code that catches an exception, the first pass (when it searches for a matching catch clause) or the think about what the exception means. Does the type you caught second pass (when it unwinds each frame’s state and executes fi- match what your program (and the libraries it uses) is documented nally and fault blocks). to throw? Do you know how to handle the exception such that your The finally block exists in order to guarantee that code always program can correctly and safely continue execution? runs, whether or not there is an exception. (Fault blocks run only Exception handling is a powerful tool that should be used carewhen an exception occurs, but they have a similar guarantee of fully and thoughtfully. If you really need to use this feature—if you always being executed.) These constructs are used to clean up really need to handle exceptions that may indicate a corrupt procritical resources such as releasing file handles or reversing im- cess—the CLR will trust you and let you do so. Just be careful and personation contexts. do it correctly. Even code that is written to be reliable through the use of constrained execution regions (CER) won’t be executed when a CSE Andrew PArdoe is a program manager for CLR at Microsoft. He works on many has been raised unless it is in a function that has been marked with aspects of the execution engine for both the desktop and Silverlight runtimes. He the HandleProcessCorruptedStateExceptions attribute. It’s very dif- can be reached at Andrew.Pardoe@microsoft.com. msdnmagazine.com February 2009 21 http://msdn.microsoft.com/magazine/cc163298 http://www.msdnmagazine.com
Table of Contents Feed for the Digital Edition of MSDN Magazine - February 2009 MSDN Magazine - February 2009 Contents Toolbox CLR Inside Out Data Points Cutting Edge Patterns In Practice Best Practices .Net Interop "Oslo" Basics Patterns Silverlight Under The Table Foundations Windows With C++ .NET Matters Going Places { End Bracket } MSDN Magazine - February 2009 MSDN Magazine - February 2009 - (Page Splash1) MSDN Magazine - February 2009 - Contents (Page Cover1) MSDN Magazine - February 2009 - Contents (Page Cover2) MSDN Magazine - February 2009 - Contents (Page 1) MSDN Magazine - February 2009 - Contents (Page 2) MSDN Magazine - February 2009 - Contents (Page 3) MSDN Magazine - February 2009 - Contents (Page 4) MSDN Magazine - February 2009 - Contents (Page 5) MSDN Magazine - February 2009 - Contents (Page 6) MSDN Magazine - February 2009 - Contents (Page 7) MSDN Magazine - February 2009 - Contents (Page 8) MSDN Magazine - February 2009 - Contents (Page 9) MSDN Magazine - February 2009 - Contents (Page 10) MSDN Magazine - February 2009 - Toolbox (Page 11) MSDN Magazine - February 2009 - Toolbox (Page 12) MSDN Magazine - February 2009 - Toolbox (Page 13) MSDN Magazine - February 2009 - Toolbox (Page 14) MSDN Magazine - February 2009 - CLR Inside Out (Page 15) MSDN Magazine - February 2009 - CLR Inside Out (Page 16) MSDN Magazine - February 2009 - CLR Inside Out (Page 17) MSDN Magazine - February 2009 - CLR Inside Out (Page 18) MSDN Magazine - February 2009 - CLR Inside Out (Page 19) MSDN Magazine - February 2009 - CLR Inside Out (Page 20) MSDN Magazine - February 2009 - CLR Inside Out (Page 21) MSDN Magazine - February 2009 - CLR Inside Out (Page 22) MSDN Magazine - February 2009 - Data Points (Page 23) MSDN Magazine - February 2009 - Data Points (Page 24) MSDN Magazine - February 2009 - Data Points (Page 25) MSDN Magazine - February 2009 - Data Points (Page 26) MSDN Magazine - February 2009 - Data Points (Page 27) MSDN Magazine - February 2009 - Data Points (Page 28) MSDN Magazine - February 2009 - Data Points (Page 29) MSDN Magazine - February 2009 - Data Points (Page 30) MSDN Magazine - February 2009 - Cutting Edge (Page 31) MSDN Magazine - February 2009 - Cutting Edge (Page 32) MSDN Magazine - February 2009 - Cutting Edge (Page 33) MSDN Magazine - February 2009 - Cutting Edge (Page 34) MSDN Magazine - February 2009 - Cutting Edge (Page 35) MSDN Magazine - February 2009 - Cutting Edge (Page 36) MSDN Magazine - February 2009 - Cutting Edge (Page 37) MSDN Magazine - February 2009 - Cutting Edge (Page 38) MSDN Magazine - February 2009 - Patterns In Practice (Page 39) MSDN Magazine - February 2009 - Patterns In Practice (Page 40) MSDN Magazine - February 2009 - Patterns In Practice (Page 41) MSDN Magazine - February 2009 - Patterns In Practice (Page 42) MSDN Magazine - February 2009 - Patterns In Practice (Page 43) MSDN Magazine - February 2009 - Patterns In Practice (Page 44) MSDN Magazine - February 2009 - Patterns In Practice (Page 45) MSDN Magazine - February 2009 - Best Practices (Page 46) MSDN Magazine - February 2009 - Best Practices (Page 47) MSDN Magazine - February 2009 - Best Practices (Page 48) MSDN Magazine - February 2009 - Best Practices (Page 49) MSDN Magazine - February 2009 - Best Practices (Page 50) MSDN Magazine - February 2009 - Best Practices (Page 51) MSDN Magazine - February 2009 - Best Practices (Page 52) MSDN Magazine - February 2009 - Best Practices (Page 53) MSDN Magazine - February 2009 - Best Practices (Page 54) MSDN Magazine - February 2009 - Best Practices (Page 55) MSDN Magazine - February 2009 - Best Practices (Page 56) MSDN Magazine - February 2009 - .Net Interop (Page 57) MSDN Magazine - February 2009 - .Net Interop (Page 58) MSDN Magazine - February 2009 - .Net Interop (Page 59) MSDN Magazine - February 2009 - .Net Interop (Page 60) MSDN Magazine - February 2009 - .Net Interop (Page 61) MSDN Magazine - February 2009 - .Net Interop (Page 62) MSDN Magazine - February 2009 - "Oslo" Basics (Page 63) MSDN Magazine - February 2009 - "Oslo" Basics (Page 64) MSDN Magazine - February 2009 - "Oslo" Basics (Page 65) MSDN Magazine - February 2009 - "Oslo" Basics (Page 66) MSDN Magazine - February 2009 - "Oslo" Basics (Page 67) MSDN Magazine - February 2009 - "Oslo" Basics (Page 68) MSDN Magazine - February 2009 - "Oslo" Basics (Page 69) MSDN Magazine - February 2009 - "Oslo" Basics (Page 70) MSDN Magazine - February 2009 - "Oslo" Basics (Page 71) MSDN Magazine - February 2009 - Patterns (Page 72) MSDN Magazine - February 2009 - Patterns (Page 73) MSDN Magazine - February 2009 - Patterns (Page 74) MSDN Magazine - February 2009 - Patterns (Page 75) MSDN Magazine - February 2009 - Patterns (Page 76) MSDN Magazine - February 2009 - Patterns (Page 77) MSDN Magazine - February 2009 - Patterns (Page 78) MSDN Magazine - February 2009 - Patterns (Page 79) MSDN Magazine - February 2009 - Patterns (Page 80) MSDN Magazine - February 2009 - Patterns (Page 81) MSDN Magazine - February 2009 - Patterns (Page 82) MSDN Magazine - February 2009 - Patterns (Page 83) MSDN Magazine - February 2009 - Silverlight (Page 84) MSDN Magazine - February 2009 - Silverlight (Page 85) MSDN Magazine - February 2009 - Silverlight (Page 86) MSDN Magazine - February 2009 - Silverlight (Page 87) MSDN Magazine - February 2009 - Silverlight (Page 88) MSDN Magazine - February 2009 - Silverlight (Page 89) MSDN Magazine - February 2009 - Silverlight (Page 90) MSDN Magazine - February 2009 - Silverlight (Page 91) MSDN Magazine - February 2009 - Silverlight (Page 92) MSDN Magazine - February 2009 - Silverlight (Page 93) MSDN Magazine - February 2009 - Silverlight (Page 94) MSDN Magazine - February 2009 - Under The Table (Page 95) MSDN Magazine - February 2009 - Under The Table (Page 96) MSDN Magazine - February 2009 - Under The Table (Page 97) MSDN Magazine - February 2009 - Under The Table (Page 98) MSDN Magazine - February 2009 - Under The Table (Page 99) MSDN Magazine - February 2009 - Under The Table (Page 100) MSDN Magazine - February 2009 - Foundations (Page 101) MSDN Magazine - February 2009 - Foundations (Page 102) MSDN Magazine - February 2009 - Foundations (Page 103) MSDN Magazine - February 2009 - Foundations (Page 104) MSDN Magazine - February 2009 - Foundations (Page 105) MSDN Magazine - February 2009 - Foundations (Page 106) MSDN Magazine - February 2009 - Windows With C++ (Page 107) MSDN Magazine - February 2009 - Windows With C++ (Page 108) MSDN Magazine - February 2009 - Windows With C++ (Page 109) MSDN Magazine - February 2009 - Windows With C++ (Page 110) MSDN Magazine - February 2009 - .NET Matters (Page 111) MSDN Magazine - February 2009 - .NET Matters (Page 112) MSDN Magazine - February 2009 - .NET Matters (Page 113) MSDN Magazine - February 2009 - .NET Matters (Page 114) MSDN Magazine - February 2009 - Going Places (Page 115) MSDN Magazine - February 2009 - Going Places (Page 116) MSDN Magazine - February 2009 - Going Places (Page 117) MSDN Magazine - February 2009 - Going Places (Page 118) MSDN Magazine - February 2009 - Going Places (Page 119) MSDN Magazine - February 2009 - { End Bracket } (Page 120) MSDN Magazine - February 2009 - { End Bracket } (Page Cover3) MSDN Magazine - February 2009 - { End Bracket } (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.