<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Getting more information than the exception class provides</title><link>http://blogs.msdn.com/clrteam/archive/2009/06/19/getting-more-information-than-the-exception-class-provides.aspx</link><description>We recently had a question about how to get more information than an exception’s type provides. The developer was trying to copy a file and didn’t know why the copy was failing. File copies can fail for many reasons, almost all of them what Eric Lippert</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Need access to native OS errors from .Net? Marshal.GetLastWin32Error() is your friend &amp;laquo;  C# Hacker - The Rambling Coder</title><link>http://blogs.msdn.com/clrteam/archive/2009/06/19/getting-more-information-than-the-exception-class-provides.aspx#9793636</link><pubDate>Sat, 20 Jun 2009 16:18:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9793636</guid><dc:creator>Need access to native OS errors from .Net? Marshal.GetLastWin32Error() is your friend &amp;laquo;  C# Hacker - The Rambling Coder</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://www.csharphacker.com/technicalblog/index.php/2009/06/20/need-access-to-native-os-errors-from-net-marshalgetlastwin32error-is-your-friend/"&gt;http://www.csharphacker.com/technicalblog/index.php/2009/06/20/need-access-to-native-os-errors-from-net-marshalgetlastwin32error-is-your-friend/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Getting more information than the exception class provides</title><link>http://blogs.msdn.com/clrteam/archive/2009/06/19/getting-more-information-than-the-exception-class-provides.aspx#9793813</link><pubDate>Sat, 20 Jun 2009 18:04:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9793813</guid><dc:creator>Jeff LeBert</dc:creator><description>&lt;p&gt;I use Marshal.GetHRForException(e) instead. I was using FileStream and WebClient to open IO streams. Both throw completely different exceptions for the &amp;quot;same&amp;quot; issue. I created a function that would look at the exception and tell me if it was a file not found, authorization, sharing issue, etc. Obviously GetLastWin32Error wouldn't work because I wanted to look at a specific exception.&lt;/p&gt;
</description></item><item><title>Getting more information than the exception class provides - CLR Team Blog</title><link>http://blogs.msdn.com/clrteam/archive/2009/06/19/getting-more-information-than-the-exception-class-provides.aspx#9793986</link><pubDate>Sun, 21 Jun 2009 00:44:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9793986</guid><dc:creator>DotNetShoutout</dc:creator><description>&lt;p&gt;Thank you for submitting this cool story - Trackback from DotNetShoutout&lt;/p&gt;
</description></item><item><title>re: Getting more information than the exception class provides</title><link>http://blogs.msdn.com/clrteam/archive/2009/06/19/getting-more-information-than-the-exception-class-provides.aspx#9795925</link><pubDate>Sun, 21 Jun 2009 20:57:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9795925</guid><dc:creator>Dmitry Zaslavsky</dc:creator><description>&lt;p&gt;int error = Marshal.GetLastWin32Error();&lt;/p&gt;
&lt;p&gt;Is better then parsing text of course. However it relies on the fact that at least BCL itself will never call another API which will reset the code. &lt;/p&gt;
&lt;p&gt;This effectively relies on the internals of BCL not to change and it also would be a possible breaking change in the future. &lt;/p&gt;
&lt;p&gt;For example you may decide to call some other APIs to get more information about the source of the issue.&lt;/p&gt;
&lt;p&gt;I wouldn't recommend this solution either.&lt;/p&gt;
&lt;p&gt;In short, I think Marshal.GetLastWin32Error() is just a tiny bit better than parsing text.&lt;/p&gt;
&lt;p&gt;Full solution is for BCL to subsclass exceptions or at the very least to expose Win32Code on the exception thrown&lt;/p&gt;
</description></item><item><title>re: Getting more information than the exception class provides</title><link>http://blogs.msdn.com/clrteam/archive/2009/06/19/getting-more-information-than-the-exception-class-provides.aspx#9795939</link><pubDate>Sun, 21 Jun 2009 21:08:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9795939</guid><dc:creator>Andrei Rinea</dc:creator><description>&lt;p&gt;To nitpick a little, in the code sample since the first FileStream result is not used anywhere else in the code in Release compilation it could be collected and finalized (and therefore the file closed).&lt;/p&gt;
&lt;p&gt;I know it is just a simple code sample + it's very unlikely to have the file closed by the time the second File.Open gets called.&lt;/p&gt;
&lt;p&gt;I just could not resist nitpicking :)&lt;/p&gt;
</description></item><item><title>re: Getting more information than the exception class provides</title><link>http://blogs.msdn.com/clrteam/archive/2009/06/19/getting-more-information-than-the-exception-class-provides.aspx#9796418</link><pubDate>Mon, 22 Jun 2009 01:49:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9796418</guid><dc:creator>Oleg Mihailik</dc:creator><description>&lt;p&gt;Certainly if you want to differentiate errors, you &amp;nbsp;create inherited exception class. One would have thought CLR team knows the guidelines of programming CLR.&lt;/p&gt;
&lt;p&gt;I cant wait to laugh when you realize Win32Code is not detailed enough for some scenarios, and you will add one more differentiator like Win32SubCode. Then Win64SubCode, Ole32Code and whatelse.&lt;/p&gt;
&lt;p&gt;And if you're wary of changing class hierarchies, didn't you already add Dictionary-typed property to Exception?&lt;/p&gt;
</description></item><item><title>re: Getting more information than the exception class provides</title><link>http://blogs.msdn.com/clrteam/archive/2009/06/19/getting-more-information-than-the-exception-class-provides.aspx#9796457</link><pubDate>Mon, 22 Jun 2009 02:19:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9796457</guid><dc:creator>Oleg Mihailik</dc:creator><description>&lt;p&gt;&amp;gt;&amp;gt; I know it is just a simple code sample + it's very unlikely to have the file closed&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt; by the time the second File.Open gets called.&lt;/p&gt;
&lt;p&gt;Well, since the first FileStream instance is not cached in a variable, it is as likely to get nicked in Debug mode as in Release.&lt;/p&gt;
&lt;p&gt;I do not think that is a nitpicking at all. It is rather likely the whole problem of requiring Win32Code was in fact sucked out of that bizarre code style. Let me explain.&lt;/p&gt;
&lt;p&gt;Imagine, somebody uses File.Open this childish way:&lt;/p&gt;
&lt;p&gt;var f = File.Open(...);&lt;/p&gt;
&lt;p&gt;f.Read(...);&lt;/p&gt;
&lt;p&gt;f.Close();&lt;/p&gt;
&lt;p&gt;Obviously, sometimes File.Open may fail by host of practical reasons: USB stick is pulled out in the middle of write, CD is too scratchy, network gone dead, actual intrinsic code logic error not related to I/O. Nobody really cares about the reason in that situation: you just can't recover.&lt;/p&gt;
&lt;p&gt;However, that file is left opened. That is where 'derivative' error started to happen when accessing the same path. If Garbage Collector was lazy enough to not clean the file handle in time.&lt;/p&gt;
&lt;p&gt;And only that 'derivative' error is recoverable. Surely, if you sit and wait long enough, GC breaks in, start collecting dead bodies and switching off life machines of those poor comatose guys who do not have living relations or next-of-kin to remember them. Alas, waiting may help when you've been dirty not cleaning up your own stuff, and there's GC willing to clean anyway.&lt;/p&gt;
&lt;p&gt;Why don't I think that file sharing issue is genuine need? Because in real life blocked file &lt;/p&gt;
&lt;p&gt;is as much likely to get released, as scratched CD get read be second attempt, or network get reconnected after you walk out of the underground walkway. So if you genuinely do want to do several File.Open attempts, you would want to do that not only for blocked shared file.&lt;/p&gt;
&lt;p&gt;I know that's kind of psychic debugging, but common sense and logic give strong suggestion I've guessed it right.&lt;/p&gt;
&lt;p&gt;In general, there's no justified reason to have this feature. Of course there's 'dirty coding' reason, but should BCL get messy and more complex to help somebody hack their way through bugs? Don't think so.&lt;/p&gt;
&lt;p&gt;If they do want to keep to their muddy paths, why don't they learn how to use PInvoke and get files opened with kernel32.dll/CreateFile?&lt;/p&gt;
</description></item><item><title>re: Getting more information than the exception class provides</title><link>http://blogs.msdn.com/clrteam/archive/2009/06/19/getting-more-information-than-the-exception-class-provides.aspx#9803712</link><pubDate>Thu, 25 Jun 2009 18:20:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9803712</guid><dc:creator>Peter Ritchie</dc:creator><description>&lt;p&gt;It would be nice if the Framework used HResult consistently. &amp;nbsp;Sometimes the framework uses HResult to communicate the last win32 error, sometimes it doesn't.&lt;/p&gt;
&lt;p&gt;It would actually be more useful if the Framework used IOException consistently, i.e. don't use IOException for invalid-operation-like exceptions (e.g. Directory.Move when source and dest don't have same root. &amp;nbsp;That's not an IO exception...)&lt;/p&gt;
</description></item><item><title>re: Getting more information than the exception class provides</title><link>http://blogs.msdn.com/clrteam/archive/2009/06/19/getting-more-information-than-the-exception-class-provides.aspx#9894907</link><pubDate>Mon, 14 Sep 2009 13:49:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9894907</guid><dc:creator>qs7000</dc:creator><description>&lt;p&gt;Let me direct your attention to the following feedback entry in Connect:&lt;/p&gt;
&lt;p&gt;&amp;quot;IOException should provide more differentiated error information&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=94306"&gt;https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=94306&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;What surprised me is its age: it was created in 2005, and nothing seems to have happened since.&lt;/p&gt;
&lt;p&gt;Please add some new IOException-derived types to .Net 4.0 - this is long overdue (especially SharingViolationException)!&lt;/p&gt;
</description></item></channel></rss>