{"id":2863,"date":"2005-09-20T13:00:00","date_gmt":"2005-09-20T13:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/heaths\/2005\/09\/20\/catching-exceptions-in-jscript-net\/"},"modified":"2005-09-20T13:00:00","modified_gmt":"2005-09-20T13:00:00","slug":"catching-exceptions-in-jscript-net","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/setup\/catching-exceptions-in-jscript-net\/","title":{"rendered":"Catching Exceptions in JScript.NET"},"content":{"rendered":"<p><a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/jscript7\/html\/jsoriJScript.asp\">JScript.NET<\/a> was created to be compatible with <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/script56\/html\/js56jsoriJScript.asp\">JScript<\/a> while benefiting from and providing access to more robust features of the .NET Framework. If you&#8217;re accustomed to more oft-used managed languages like C# and VB.NET, catching different types of exception classes should be no stranger. Considering JScript.NET, however, what would you expect to be printed from the code below?<\/p>\n<pre><font color=\"blue\">import<\/font> System;\n<font color=\"blue\">try<\/font>\n{\n&nbsp;&nbsp;<font color=\"blue\">throw<\/font> <font color=\"blue\">new<\/font> Exception(\"Error: something bad happened.\");\n}\n<font color=\"blue\">catch<\/font> (e)\n{\n&nbsp;&nbsp;<font color=\"blue\">if<\/font> (e <font color=\"blue\">instanceof<\/font> Exception)\n&nbsp;&nbsp;&nbsp;&nbsp;print(e);\n}<\/pre>\n<p>If you said nothing then you&#8217;re correct. Exceptions are wrapped in an <code>Microsoft.JScript.ErrorObject<\/code> that does not extend <code>System.Exception<\/code>. You need to extract the exception from the <code>ErrorObject<\/code> if you&#8217;d like to handle different classes of exceptions. One way to do this is using a public static method that JScript.NET uses to throw exceptions for types other than <code>Exception<\/code> and <code>ErrorObject<\/code>.<\/p>\n<pre><font color=\"blue\">import<\/font> System;\n<font color=\"blue\">import<\/font> Microsoft.JScript;\n<font color=\"blue\">try<\/font>\n{\n&nbsp;&nbsp;<font color=\"blue\">throw<\/font> <font color=\"blue\">new<\/font> Exception(\"Error: something bad happened.\");\n}\n<font color=\"blue\">catch<\/font> (e)\n{\n&nbsp;&nbsp;<font color=\"blue\">var<\/font> ex = Throw.JScriptThrow(e);\n&nbsp;&nbsp;<font color=\"blue\">if<\/font> (ex <font color=\"blue\">instanceof<\/font> Exception)\n&nbsp;&nbsp;&nbsp;&nbsp;print(e);\n}<\/pre>\n<p>You can also call the explicit cast operator defined for the <code>ErrorObject<\/code> class, but make sure you&#8217;re dealing with an <code>ErrorObject<\/code> first in case another type is being thrown because the <code>castclass<\/code> IL instruction is used to cast the exception variable to <code>ErrorObject<\/code>.<\/p>\n<pre><font color=\"blue\">import<\/font> System;\n<font color=\"blue\">import<\/font> Microsoft.JScript;\n<font color=\"blue\">try<\/font>\n{\n&nbsp;&nbsp;<font color=\"blue\">throw<\/font> <font color=\"blue\">new<\/font> Exception(\"Error: something bad happened.\");\n}\n<font color=\"blue\">catch<\/font> (e)\n{\n&nbsp;&nbsp;<font color=\"blue\">if<\/font> (e <font color=\"blue\">instanceof<\/font> ErrorObject)\n&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;<font color=\"blue\">var<\/font> ex : Exception = ErrorObject.op_Explicit(e);\n&nbsp;&nbsp;&nbsp;&nbsp;<font color=\"blue\">if<\/font> (ex)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(e);\n&nbsp;&nbsp;}\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>JScript.NET was created to be compatible with JScript while benefiting from and providing access to more robust features of the .NET Framework. If you&#8217;re accustomed to more oft-used managed languages like C# and VB.NET, catching different types of exception classes should be no stranger. Considering JScript.NET, however, what would you expect to be printed from [&hellip;]<\/p>\n","protected":false},"author":389,"featured_media":3843,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[34],"class_list":["post-2863","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-script"],"acf":[],"blog_post_summary":"<p>JScript.NET was created to be compatible with JScript while benefiting from and providing access to more robust features of the .NET Framework. If you&#8217;re accustomed to more oft-used managed languages like C# and VB.NET, catching different types of exception classes should be no stranger. Considering JScript.NET, however, what would you expect to be printed from [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/posts\/2863","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/users\/389"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/comments?post=2863"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/posts\/2863\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/media\/3843"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/media?parent=2863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/categories?post=2863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/setup\/wp-json\/wp\/v2\/tags?post=2863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}