{"id":23913,"date":"2008-01-07T10:00:00","date_gmt":"2008-01-07T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2008\/01\/07\/clean-up-functions-cant-fail-because-well-how-do-you-clean-up-from-a-failed-clean-up\/"},"modified":"2008-01-07T10:00:00","modified_gmt":"2008-01-07T10:00:00","slug":"clean-up-functions-cant-fail-because-well-how-do-you-clean-up-from-a-failed-clean-up","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20080107-00\/?p=23913","title":{"rendered":"Clean-up functions can&#039;t fail because, well, how do you clean up from a failed clean-up?"},"content":{"rendered":"<p>Commenter Matt asks <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/pages\/407234.aspx#450040\"> how you&#8217;re supposed to handle failures in functions like <code>fclose<\/code> or <code>CloseHandle<\/code><\/a>. Obviously, you can&#8217;t. If a clean-up function fails, there&#8217;s not much you can do because, well, how do you clean up from a failed clean-up?\n These clean-up functions fall into the category of &#8220;Must not fail for reasons beyond the program&#8217;s control.&#8221; If a program tries to close a file and it gets an error back, what can it do? Practically speaking, nothing. The only way a clean-up function can fail is if the program fundamentally screws up, say by attempting to close something that was never open or otherwise passing an invalid parameter. It&#8217;s not like a program can try to close the handle again (or worse go into loop closing the handle repeatedly until it finally closes).\n Remember this when writing your own clean-up functions. Assuming the parameters are valid, a clean-up function must succeed.\n (I will now ruin my rhetorical flourish by yammering about the <code>fclose<\/code> function because if I don&#8217;t, people will bring it up in the comments anyway. The <code>fclose<\/code> function does extra work before closing, and that extra work may indeed run into problems, but the important thing is that when <code>fclose<\/code> returns, the stream is well and truly closed.)<\/p>\n<p> <b>Addendum<\/b>: Once again I wish to emphasize that while it may be possible for functions like <code>fclose<\/code> to run into errors while they are closing the stream, the point is that the result of the call to <code>fclose<\/code> is always a closed stream. I think most of the comments are losing sight of the point of my article and rat-holding on the various ways <code>fclose<\/code> can run into errors. That&#8217;s not the same as <i>failing<\/i>. It never fails; it always succeeds, but possibly with errors. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Commenter Matt asks how you&#8217;re supposed to handle failures in functions like fclose or CloseHandle. Obviously, you can&#8217;t. If a clean-up function fails, there&#8217;s not much you can do because, well, how do you clean up from a failed clean-up? These clean-up functions fall into the category of &#8220;Must not fail for reasons beyond the [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-23913","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Commenter Matt asks how you&#8217;re supposed to handle failures in functions like fclose or CloseHandle. Obviously, you can&#8217;t. If a clean-up function fails, there&#8217;s not much you can do because, well, how do you clean up from a failed clean-up? These clean-up functions fall into the category of &#8220;Must not fail for reasons beyond the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/23913","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=23913"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/23913\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=23913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=23913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=23913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}