{"id":113,"date":"2007-11-02T10:30:29","date_gmt":"2007-11-02T15:30:29","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2007\/11\/02\/how-to-safely-try-unknown-code\/"},"modified":"2007-11-07T08:35:46","modified_gmt":"2007-11-07T13:35:46","slug":"how-to-safely-try-unknown-code","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2007\/11\/02\/how-to-safely-try-unknown-code\/","title":{"rendered":"How to Safely Try Unknown Code"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>In my recent post on <a href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/10\/26\/exceptional-behavior\/\"><tt>MExceptions<\/tt><\/a>, a user asked if a syntax error inside a function being called in a <tt>try\/catch<\/tt> would get caught in older versions of MATLAB.  I've done a tiny bit of investigation on this and report it here.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Syntax Error is Caught<\/a><\/li>\r\n         <li><a href=\"#3\">Unsupported Syntax<\/a><\/li>\r\n         <li><a href=\"#5\">Why?<\/a><\/li>\r\n         <li><a href=\"#7\">Thoughts?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Syntax Error is Caught<a name=\"1\"><\/a><\/h3>\r\n   <p>In this first example, I have an outright syntax error in my code.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">dbtype <span style=\"color: #A020F0\">err1<\/span><\/pre><pre style=\"font-style:oblique\">\r\n1     function err1(\r\n2     \r\n3     a = 3\r\n4     b = 2\r\n<\/pre><p>You can see that MATLAB can tolerate this, and indeed, seems to tolerate these sorts of errors in a <tt>try\/catch<\/tt> context at least as far back as MATLAB version 5.3 (Release 11).\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">try<\/span>\r\n    err1\r\n<span style=\"color: #0000FF\">catch<\/span>\r\n    disp <span style=\"color: #A020F0\">'Landed in catch'<\/span>\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">Landed in catch\r\n<\/pre><h3>Unsupported Syntax<a name=\"3\"><\/a><\/h3>\r\n   <p>Similarly, for syntax that is not possible in MATLAB R2007b (the version used here), again the code jumps to the <tt>catch<\/tt> block.  In the following code, you see that I try to index into the result of a function without assigning the function output\r\n      itself to a variable.  This is not something supported in MATLAB R2007b (and I have no statement to make about the future\r\n      of this proposed construct except to say that it is an enhancement in our database and will be considered for the future).\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">dbtype <span style=\"color: #A020F0\">notPossibleR2007b<\/span><\/pre><pre style=\"font-style:oblique\">\r\n1     function notPossibleR2007b()\r\n2     \r\n3     a = eig(rand(3))(2)\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">try<\/span>\r\n    notPossibleR2007b;\r\n<span style=\"color: #0000FF\">catch<\/span>\r\n    disp <span style=\"color: #A020F0\">'Got caught'<\/span>\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">Got caught\r\n<\/pre><h3>Why?<a name=\"5\"><\/a><\/h3>\r\n   <p>Why might you wish to do this?  One reason is if you are accepting user input or code for your application.  If the code presented\r\n      to you has something in it that won't work, you might still like to control the presentation of the error to your user.  <tt>try\/catch<\/tt> with the offending code in a separate file allows you to intercede and do just that.\r\n   <\/p>\r\n   <p>Another reason to structure code this way is to allow you to take advantage of a new feature if it's available to your user.\r\n       If not, do things the older, perhaps harder or more time- or memory-consuming way.\r\n   <\/p>\r\n   <h3>Thoughts?<a name=\"7\"><\/a><\/h3>\r\n   <p>Do you need to \"support\" multiple versions of MATLAB?  Will separating your code this way help?  Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=113#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_937fffe2bf9e47a694a4511de89ac861() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='937fffe2bf9e47a694a4511de89ac861 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 937fffe2bf9e47a694a4511de89ac861';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Loren Shure';\r\n        copyright = 'Copyright 2007 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_937fffe2bf9e47a694a4511de89ac861()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.5<br><\/p>\r\n<\/div>\r\n<!--\r\n937fffe2bf9e47a694a4511de89ac861 ##### SOURCE BEGIN #####\r\n%% How to Safely Try Unknown Code\r\n% In my recent post on\r\n% <https:\/\/blogs.mathworks.com\/loren\/2007\/10\/26\/exceptional-behavior\/ |MExceptions|>, \r\n% a user asked if a syntax error inside a function being called in a\r\n% |try\/catch| would get caught in older versions of MATLAB.  I've done a\r\n% tiny bit of investigation on this and report it here.\r\n%% Syntax Error is Caught\r\n% In this first example, I have an outright syntax error in my code.\r\ndbtype err1\r\n%%\r\n% You can see that MATLAB can tolerate this, and indeed, seems to tolerate\r\n% these sorts of errors in a |try\/catch| context at least as far back as\r\n% MATLAB version 5.3 (Release 11).\r\ntry\r\n    err1\r\ncatch\r\n    disp 'Landed in catch'\r\nend\r\n%% Unsupported Syntax\r\n% Similarly, for syntax that is not possible in MATLAB R2007b (the version\r\n% used here), again the code jumps to the |catch| block.  In the following\r\n% code, you see that I try to index into the result of a function without\r\n% assigning the function output itself to a variable.  This is not\r\n% something supported in MATLAB R2007b (and I have no statement to make\r\n% about the future of this proposed construct except to say that it is an\r\n% enhancement in our database and will be considered for the future).\r\ndbtype notPossibleR2007b\r\n%%\r\n%\r\ntry\r\n    notPossibleR2007b;\r\ncatch\r\n    disp 'Got caught'\r\nend\r\n%%  Why?\r\n% Why might you wish to do this?  One reason is if you are accepting user\r\n% input or code for your application.  If the code presented to you has\r\n% something in it that won't work, you might still like to control the\r\n% presentation of the error to your user.  |try\/catch| with the offending\r\n% code in a separate file allows you to intercede and do just that.\r\n%%\r\n% Another reason to structure code this way is to allow you to take\r\n% advantage of a new feature if it's available to your user.  If not, do\r\n% things the older, perhaps harder or more time- or memory-consuming way.\r\n%% Thoughts?\r\n% Do you need to \"support\" multiple versions of MATLAB?  Will separating\r\n% your code this way help?  Let me know\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=113#respond here>.\r\n##### SOURCE END ##### 937fffe2bf9e47a694a4511de89ac861\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      In my recent post on MExceptions, a user asked if a syntax error inside a function being called in a try\/catch would get caught in older versions of MATLAB.  I've done a tiny bit of... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/11\/02\/how-to-safely-try-unknown-code\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,15,11],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/113"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=113"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/113\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}