{"id":7151,"date":"2014-05-26T07:47:35","date_gmt":"2014-05-26T07:47:35","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/05\/26\/how-to-check-if-ioexception-is-not-enough-disk-space-exception-type-collection-of-common-programming-errors\/"},"modified":"2014-05-26T07:47:35","modified_gmt":"2014-05-26T07:47:35","slug":"how-to-check-if-ioexception-is-not-enough-disk-space-exception-type-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/05\/26\/how-to-check-if-ioexception-is-not-enough-disk-space-exception-type-collection-of-common-programming-errors\/","title":{"rendered":"How to check if IOException is Not-Enough-Disk-Space-Exception type?-Collection of common programming errors"},"content":{"rendered":"<p>Well, it&#8217;s a bit hacky, but here we go.<\/p>\n<p>First thing to do is to get the <code>HResult<\/code> from the exception. As it&#8217;s a protected member, we need a bit of reflection to get the value. Here&#8217;s an extension method to will do the trick:<\/p>\n<pre><code>public static class ExceptionExtensions\n{\n    public static int HResultPublic(this Exception exception)\n    {\n        var hResult = exception.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).Where(z =&gt; z.Name.Equals(\"HResult\")).First();\n        return (int)hResult.GetValue(exception, null);\n    }\n}\n<\/code><\/pre>\n<p>Now, in your catch scope, you can get the <code>HResult<\/code>:<\/p>\n<pre><code>catch (Exception ex)\n{\n    int hResult = ex.HResultPublic();\n}\n<\/code><\/pre>\n<p>From here, you&#8217;ll have to interpret the HResult. You&#8217;ll need this link.<\/p>\n<p>We need to get the <code>ErrorCode<\/code> which is stored in the 16 first bits of the value, so here&#8217;s some bit operation:<\/p>\n<pre><code>int errorCode = (int)(hResult &amp; 0x0000FFFF);\n<\/code><\/pre>\n<p>Now, refer to the list of system error codes and here we are:<\/p>\n<pre><code>ERROR_DISK_FULL\n112 (0x70)\n<\/code><\/pre>\n<p>So test it using:<\/p>\n<pre><code>switch (errorCode)\n{\n    case 112:\n        \/\/ Disk full\n}\n<\/code><\/pre>\n<p>Maybe there are some &#8220;higher level&#8221; functions to get all this stuff, but at least it works.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Well, it&#8217;s a bit hacky, but here we go. First thing to do is to get the HResult from the exception. As it&#8217;s a protected member, we need a bit of reflection to get the value. Here&#8217;s an extension method to will do the trick: public static class ExceptionExtensions { public static int HResultPublic(this Exception [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7151","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7151"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7151\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}