{"id":3403,"date":"2025-04-18T06:38:53","date_gmt":"2025-04-18T06:38:53","guid":{"rendered":"https:\/\/webprojects.cloud\/wordpress\/splatco\/?post_type=spl_knowledgebase&#038;p=3403"},"modified":"2025-06-10T11:06:44","modified_gmt":"2025-06-10T11:06:44","slug":"exception-handling","status":"publish","type":"spl_knowledgebase","link":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-splat-call-home-sx10509-programming\/example-splat-call-home-sandbox-program\/exception-handling\/","title":{"rendered":"Exception handling"},"content":{"rendered":"\n<p>Exception handling is what we include in a program to make it robust enough to withstand things going wrong, be they expected or unexpected. For example, what happens if we send a GET to the server but the server fails to respond? A badly written program might totally lock up. With good exception handling it continues to perform its other chores and pick up on the server communications when restored. Think &#8220;graceful degradation&#8221;.<\/p>\n\n\n\n<p>The sandbox program presented so far has no exception handling. I deliberately left it out to keep the main functions as simple as possible to follow.<\/p>\n\n\n\n<p>The main thing that could go wrong with Internet communications is that the connection is lost, be it for seconds or forever. It can happen at any time on the Internet. Our program should be able to take dropouts in its stride.<\/p>\n\n\n\n<p>One strategy for a program such as this that helps to make it robust, is to have all the Internet stuff in one or more dedicated MultiTrack tasks. Say this sample program is part of your sausage machine controller. If the Internet fails, you still want to make sausages &#8211; you may simply lose the remote readout of how many sausages have been produced. If the counting and reporting is in quite separate tasks, a failure of the Internet will not stop production.<\/p>\n\n\n\n<p>Just isolating the Internet stuff in separate tasks is not enough if your application relies on input from the Internet. In that case you may have to be able to detect the Internet failure and provide a default behaviour that can take over. This is called exception trapping (detecting the problem) and exception handling (doing something about it).<\/p>\n\n\n\n<p>One of the most common and versatile ways of detecting something going wrong outside the controller is by a timeout. If the program initiates some action in the real world, and the expected response fails to eventuate in a preset time, something is assumed to be wrong &#8230; there&#8217;s an exception.<\/p>\n\n\n\n<p>Timeouts are a good way of detecting Internet failures. When we send a GET or POST request to a server, we can reasonably expect a response in less than a second (but more than maybe a few tens of milliseconds). Let&#8217;s say we allow 5 seconds, to be very conservative. Our sandbox program can be modified to perform this timeout very simply. Remember the semaphore we used to signal that a command had be echoed? It&#8217;s called&nbsp;<code>sSCH_GotEcho<\/code>. Let&#8217;s add a second one, called&nbsp;<code>sSCH_GotException<\/code>, which is to be set whenever it takes more than 5 seconds to get a response (echo) from a server command. We generate&nbsp;<code>sSCH_GotException<\/code>&nbsp;by modifying the transitory task&nbsp;<code>tsk_SCH_AckWait<\/code>. Here&#8217;s the modified code (new\/changed lines are flagged with&nbsp;<code>&lt;&lt;&lt;&lt;&lt;&lt;)<\/code>:<\/p>\n\n\n\n<p><a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/cutting-and-pasting-code-from-the-splat-knowledge-base\/\">(Click here<\/a>\u00a0for some tips for working around problems with copy and paste out of Internet Explorer and HTML-help\u00a0<code>(.chm)<\/code>\u00a0files)<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sSCH_GotException       defSEM                ;<code>&lt;&lt;&lt;&lt;&lt;&lt;<\/code>\ntsk_SCH_AckWait:  \n        ClrS            sSCH_GotException     ;<code>&lt;&lt;&lt;&lt;&lt;&lt;<\/code>\n        MarkTime                              ;<code>&lt;&lt;&lt;&lt;&lt;&lt;<\/code>\nSCH_AckWait_Loop:\n        YieldTask\n        Recall          bTxCmd                ;Get back the command just set\n        Recall          bRxCmdEcho            ;read the Rx echo location\n        Compare\n        GoIfZ           SCH_AckWait_A         ;g\/ the same <code>&lt;&lt;&lt;&lt;&lt;&lt;<\/code>changed line\n        LoopIfTiming    500,SCH_AckWait_Loop  ;<code>&lt;&lt;&lt;&lt;&lt;&lt;<\/code>\n        SetS            sSCH_GotException     ;<code>&lt;&lt;&lt;&lt;&lt;&lt; Signal a timeout<\/code>\nSCH_AckWait_A:        \n        SetS            sSCH_GotEcho          ;Signal that the echo is received\n        KillTask                              ;Job done!<\/pre>\n\n\n\n<p>This version will set&nbsp;<code>sSCH_GotEcho<\/code>&nbsp;whether it has success or failure. It will only set&nbsp;<code>sSCH_GotException<\/code>&nbsp;if the command timed out.<\/p>\n\n\n\n<p>Now all we have to do is test&nbsp;<code>sSCH_GotException<\/code>&nbsp;each time we get&nbsp;<code>sSCH_GotEcho<\/code>, and jump to the code that will do something about the problem. For example, in test 1:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Test1:  \n        GoSub           SCH_Idle                ;Force the SCH into idle\n        WaitForST       sSCH_GotEcho            ;Wait for the command to be echoed  &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;or fail\n        GoIfST          sSCH_GotException,SX10509Failed                            ;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;\n        GoSub           SCH_ClearTx             ;Make sure the SX10509 Tx buffer is clear\n        WaitForST       sSCH_GotEcho            ;Wait for the command to be echoed  &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;or fail\n        GoIfST          sSCH_GotException,SX10509Failed                            ;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;\n        GoSub           SCH_Get                 ;Send a GET request to the server\n        WaitForST       sSCH_GotEcho            ;Wait for the command to be echoed  &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;or fail  \n        GoIfST          sSCH_GotException,InternetFailed                           ;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;<\/pre>\n\n\n\n<p>As you can see, we can even discriminate between a failure to communicate with the SX10509 and a failure to contact the server.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Exception handling is what we include in a program to make it robust enough to withstand things going wrong, be they expected or unexpected. For&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3387,"menu_order":10,"template":"","class_list":["post-3403","spl_knowledgebase","type-spl_knowledgebase","status-publish","hentry"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Exception handling - SPLat Controls<\/title>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Exception handling - SPLat Controls\" \/>\n<meta property=\"og:description\" content=\"Exception handling is what we include in a program to make it robust enough to withstand things going wrong, be they expected or unexpected. For...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-splat-call-home-sx10509-programming\/example-splat-call-home-sandbox-program\/exception-handling\/\" \/>\n<meta property=\"og:site_name\" content=\"SPLat Controls\" \/>\n<meta property=\"article:modified_time\" content=\"2025-06-10T11:06:44+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-splat-call-home-sx10509-programming\/example-splat-call-home-sandbox-program\/exception-handling\/\",\"url\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-splat-call-home-sx10509-programming\/example-splat-call-home-sandbox-program\/exception-handling\/\",\"name\":\"Exception handling - SPLat Controls\",\"isPartOf\":{\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/#website\"},\"datePublished\":\"2025-04-18T06:38:53+00:00\",\"dateModified\":\"2025-06-10T11:06:44+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-splat-call-home-sx10509-programming\/example-splat-call-home-sandbox-program\/exception-handling\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-splat-call-home-sx10509-programming\/example-splat-call-home-sandbox-program\/exception-handling\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-splat-call-home-sx10509-programming\/example-splat-call-home-sandbox-program\/exception-handling\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tutorials, application notes and white papers\",\"item\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Tutorial: SPLat Call Home (SX10509) programming\",\"item\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-splat-call-home-sx10509-programming\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Example: SPLat Call Home sandbox program\",\"item\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-splat-call-home-sx10509-programming\/example-splat-call-home-sandbox-program\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Exception handling\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/#website\",\"url\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/\",\"name\":\"SPLat Controls\",\"description\":\"OEM Embedded Machine Controllers\",\"publisher\":{\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/#organization\",\"name\":\"SPLat Controls\",\"url\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-content\/uploads\/2024\/10\/logo.svg\",\"contentUrl\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-content\/uploads\/2024\/10\/logo.svg\",\"caption\":\"SPLat Controls\"},\"image\":{\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Exception handling - SPLat Controls","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"Exception handling - SPLat Controls","og_description":"Exception handling is what we include in a program to make it robust enough to withstand things going wrong, be they expected or unexpected. For...","og_url":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-splat-call-home-sx10509-programming\/example-splat-call-home-sandbox-program\/exception-handling\/","og_site_name":"SPLat Controls","article_modified_time":"2025-06-10T11:06:44+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-splat-call-home-sx10509-programming\/example-splat-call-home-sandbox-program\/exception-handling\/","url":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-splat-call-home-sx10509-programming\/example-splat-call-home-sandbox-program\/exception-handling\/","name":"Exception handling - SPLat Controls","isPartOf":{"@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/#website"},"datePublished":"2025-04-18T06:38:53+00:00","dateModified":"2025-06-10T11:06:44+00:00","breadcrumb":{"@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-splat-call-home-sx10509-programming\/example-splat-call-home-sandbox-program\/exception-handling\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-splat-call-home-sx10509-programming\/example-splat-call-home-sandbox-program\/exception-handling\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-splat-call-home-sx10509-programming\/example-splat-call-home-sandbox-program\/exception-handling\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webprojects.cloud\/wordpress\/splatco\/"},{"@type":"ListItem","position":2,"name":"Tutorials, application notes and white papers","item":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/"},{"@type":"ListItem","position":3,"name":"Tutorial: SPLat Call Home (SX10509) programming","item":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-splat-call-home-sx10509-programming\/"},{"@type":"ListItem","position":4,"name":"Example: SPLat Call Home sandbox program","item":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-splat-call-home-sx10509-programming\/example-splat-call-home-sandbox-program\/"},{"@type":"ListItem","position":5,"name":"Exception handling"}]},{"@type":"WebSite","@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/#website","url":"https:\/\/webprojects.cloud\/wordpress\/splatco\/","name":"SPLat Controls","description":"OEM Embedded Machine Controllers","publisher":{"@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webprojects.cloud\/wordpress\/splatco\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/#organization","name":"SPLat Controls","url":"https:\/\/webprojects.cloud\/wordpress\/splatco\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/#\/schema\/logo\/image\/","url":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-content\/uploads\/2024\/10\/logo.svg","contentUrl":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-content\/uploads\/2024\/10\/logo.svg","caption":"SPLat Controls"},"image":{"@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-json\/wp\/v2\/spl_knowledgebase\/3403","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-json\/wp\/v2\/spl_knowledgebase"}],"about":[{"href":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-json\/wp\/v2\/types\/spl_knowledgebase"}],"author":[{"embeddable":true,"href":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-json\/wp\/v2\/users\/1"}],"up":[{"embeddable":true,"href":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-json\/wp\/v2\/spl_knowledgebase\/3387"}],"wp:attachment":[{"href":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-json\/wp\/v2\/media?parent=3403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}