{"id":213,"date":"2008-05-19T06:20:37","date_gmt":"2008-05-19T11:20:37","guid":{"rendered":"https:\/\/blogs.mathworks.com\/desktop\/2008\/05\/19\/stop-in-the-name-of-error\/"},"modified":"2008-05-19T06:20:37","modified_gmt":"2008-05-19T11:20:37","slug":"stop-in-the-name-of-error","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2008\/05\/19\/stop-in-the-name-of-error\/","title":{"rendered":"Stop in the name of error"},"content":{"rendered":"<p>Recently I was performing some maintenance on the Dependency Report tool. The report has logic to display something reasonable in the web browser even if the scanned directory contains invalid MATLAB files. To make that work, there are several try\/catch blocks that the process various errors that may be encountered. <\/p>\n<p>When I tested my new changes in a directory with some &#8220;bad&#8221; MATLAB files&#8230;let&#8217;s just say that the code was not handling the errors properly!  Normally in these situations I type:<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">dbstop <span style=\"color: #A020F0\">if caught error<\/span><\/pre>\n<p>and inspect the state of program in the editor.  However due to the recursive, iterative, and nested nature of this code, there were too many caught errors in different files to make this an efficient process. That&#8217;s when I remembered that we can stop on a particular <i>type<\/i> of error, but I could not remember the syntax to do so.<\/p>\n<p>Debug menu to the rescue! <\/p>\n<p>I clicked <b>Debug -> Stop if Errors\/Warnings&#8230;<\/b> to open the <b>Stop if Errors\/Warnings for All Files<\/b> dialog.<\/p>\n<div align=\"center\">\n<img decoding=\"async\" border=\"0\" align=\"center\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/michael_katz_stop_if_error\/stop_if_error_menu.png\" alt=\"The debug menu\"\/>\n<\/div>\n<p>Next, I needed a unique error identifier, so MATLAB would know where to halt execution. This is easy to do from the command prompt using the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/lasterror.html\"><tt>lasterror<\/tt><\/a> command:<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">\r\n>> lasterror\r\n\r\nans = \r\n\r\n       message: 'Index exceeds matrix dimensions.'\r\n    identifier: 'MATLAB:badsubscript'\r\n         stack: [1x1 struct]\r\n<\/pre>\n<p>Then, I cut and pasted the identifier into the <b>Stop if Errors\/Warnings for All Files<\/b> dialog. To follow along, Click the &#8220;Try\/Catch Errors&#8221; tab (1), select &#8220;Use Message Identifiers&#8221; (2), and click &#8220;Add&#8221; (3):<\/p>\n<div align=\"center\">\n<img decoding=\"async\" border=\"0\" align=\"center\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/michael_katz_stop_if_error\/stop_if_error_dialog.png\" alt=\"The debug menu\"\/>\n<\/div>\n<p><\/b><br \/>\nFinally, I pasted in the message I was interested in and clicked &#8220;OK&#8221;:<\/p>\n<div align=\"center\">\n<img decoding=\"async\" border=\"0\" align=\"center\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/michael_katz_stop_if_error\/stop_if_error_identifier.png\" alt=\"The debug menu\"\/>\n<\/div>\n<p>After this, I re-ran my code.  Execution stopped only when this error was caught, which allowed me to debug just this issue. <\/p>\n<p>Incidentally, the command-line syntax is shown in the dialog; hopefully I will remember that for next time! <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I was performing some maintenance on the Dependency Report tool. The report has logic to display something reasonable in the web browser even if the scanned directory contains invalid MATLAB&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2008\/05\/19\/stop-in-the-name-of-error\/\">read more >><\/a><\/p>\n","protected":false},"author":38,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/213"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/comments?post=213"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/213\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}