<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>post Take A Lickin' and Keep On Tickin'... in In other news</title>
    <link>https://www.servicenow.com/community/in-other-news/take-a-lickin-and-keep-on-tickin/ba-p/2281826</link>
    <description>&lt;P&gt;I got a call the other day from Otzi (he and I go &lt;A href="http://en.wikipedia.org/wiki/Otzi"&gt;&lt;EM&gt;way&lt;/EM&gt; back&lt;/A&gt;). He ran across a piece of code that just didn't make any sense to him:&lt;/P&gt;&lt;PRE style="margin-left: 20px; line-height: 1; color: firebrick;"&gt;&lt;BR /&gt;var ans = undefined;&lt;BR /&gt;try {&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; ans = otzi.process();&lt;BR /&gt;}&lt;BR /&gt;catch (err) {&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; gs.log(err);&lt;BR /&gt;}&lt;BR /&gt;finally {&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; if (ans === undefined)&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ans = null;&lt;BR /&gt;}&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;"What," he demanded, "does that gobbledegook mean?"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Well, Otzi, here's your answer...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;There are three parts to this. First, there's the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;try { }&lt;/SPAN&gt; block. The curly braces enclose the statements we're &lt;EM&gt;trying&lt;/EM&gt;:&lt;/P&gt;&lt;PRE style="margin-left: 20px; line-height: 1; color: firebrick;"&gt;&lt;BR /&gt;try {&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; ans = otzi.process();&lt;BR /&gt;}&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;In Otzi's example, there's only one line inside the curly braces, but there could have been any number of lines. The code inside those curly braces will execute just like it would if it &lt;EM&gt;wasn't&lt;/EM&gt; inside the curly braces, with just one difference: if something goes horribly wrong when executing that code, the next part (below) comes into play. By "horribly wrong," I mean that some kind of error occurs. For example, suppose we tried executing the code above while the variable &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;otzi&lt;/SPAN&gt; was null, or even undefined? Without the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;try { }&lt;/SPAN&gt;, you'd get an error in the log and your script would stop. With the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;try { }&lt;/SPAN&gt; block, something entirely different happens:&lt;/P&gt;&lt;PRE style="margin-left: 20px; line-height: 1; color: firebrick;"&gt;&lt;BR /&gt;catch (err) {&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; gs.log(err);&lt;BR /&gt;}&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;If an error occurs when the code inside the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;try { }&lt;/SPAN&gt; block executes, the code inside the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;catch {err}&lt;/SPAN&gt; block will execute. For example, if &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;otzi&lt;/SPAN&gt; was null when we ran the script, JavaScript would &lt;EM&gt;throw&lt;/EM&gt; an error when it tried to execute the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;.process()&lt;/SPAN&gt; method (it's kind of hard for null to have a &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;.process()&lt;/SPAN&gt; method!). When JavaScript &lt;EM&gt;throws&lt;/EM&gt; an error, it creates an instance of the &lt;A title="eveloper.mozilla.org/en/JavaScript/Reference/Global_Objects/Error" href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error"&gt;Error class&lt;/A&gt; containing a message describing the error. Then it calls the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;catch (err)&lt;/SPAN&gt; block, exactly as if &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;catch()&lt;/SPAN&gt; were a function. The code inside your &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;catch (err)&lt;/SPAN&gt; block can then read the message inside the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;Error&lt;/SPAN&gt; instance and do whatever it needs to do. In Otzi's example, all that the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;catch (err)&lt;/SPAN&gt; block is doing is logging the error — but it could do anything at all.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Finally, there's the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;finally { }&lt;/SPAN&gt; block:&lt;/P&gt;&lt;PRE style="margin-left: 20px; line-height: 1; color: firebrick;"&gt;&lt;BR /&gt;finally {&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; if (ans === undefined)&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ans = null;&lt;BR /&gt;}&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;The first thing to know about the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;finally { }&lt;/SPAN&gt; block is that it's optional — &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;try { }&lt;/SPAN&gt; blocks don't have to be followed by a &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;finally { }&lt;/SPAN&gt; block, and most of the time they aren't. When there &lt;EM&gt;is&lt;/EM&gt; a &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;finally { }&lt;/SPAN&gt; block, the code inside of it will be executed &lt;EM&gt;no matter what happens&lt;/EM&gt; when executing the code inside the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;catch { }&lt;/SPAN&gt; block. In Otzi's example, the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;finally { }&lt;/SPAN&gt; block is simply ensuring that the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;ans&lt;/SPAN&gt; variable ends up defined: even if an error occurs, even if an undefined value is returned.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;finally { }&lt;/SPAN&gt; block finishes executing (or when the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;catch { }&lt;/SPAN&gt; block finishes, if there is no &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;finally { }&lt;/SPAN&gt; block), the script keeps on executing. This is really the most important thing of all about using a &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;try { }/catch { }&lt;/SPAN&gt; construct: it's a way for your code to take a lickin' and keep on tickin'. For any situation where an error &lt;EM&gt;could&lt;/EM&gt; occur, but isn't catastrophic (and therefore doesn't warrant aborting the script's execution), the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;try { }/catch { }&lt;/SPAN&gt; construct is your friend.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A couple more details worth knowing:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If your script is calling Java code (whether through a Packages.xxx call or through a Java class exposed as a JavaScript class), and that code throws a Java exception (a mechanism within Java that's very much like the JavaScript &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;try { }/catch { }&lt;/SPAN&gt; construct), then JavaScript will turn that Java exception into a JavaScript Error and throw it. Bottom line: you can &lt;EM&gt;catch&lt;/EM&gt; one of these error just like you can catch a JavaScript Error.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;JavaScript doesn't have a monopoly on the ability to throw an error — your own code can do it. Here's a trivial example:&lt;/P&gt;&lt;PRE style="margin-left: 20px; line-height: 1; color: firebrick;"&gt;&lt;BR /&gt;var x = 0;&lt;BR /&gt;try {&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; gs.log(digits(x));&lt;BR /&gt;}&lt;BR /&gt;catch (err) {&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; gs.log(err);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;// return the number of integer digits in a positive number...&lt;BR /&gt;function digits(arg) {&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; if (arg &amp;lt;= 0)&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; throw new Error('Argument must be positive!');&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; return 1 + Math.floor(Math.LOG10E * Math.log(arg));&lt;BR /&gt;}&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;If &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;x&lt;/SPAN&gt; has a number greater than zero, we'll log the number of integer digits in that number. Otherwise our function will throw an error, we &lt;EM&gt;won't&lt;/EM&gt; log the number of digits (which we couldn't compute), and we'll log an error instead.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Are you feeling a little more enlightened now, oh ancient Otzi?&lt;/P&gt;</description>
    <pubDate>Tue, 15 Nov 2011 15:41:23 GMT</pubDate>
    <dc:creator>SlightlyLoony</dc:creator>
    <dc:date>2011-11-15T15:41:23Z</dc:date>
    <item>
      <title>Take A Lickin' and Keep On Tickin'...</title>
      <link>https://www.servicenow.com/community/in-other-news/take-a-lickin-and-keep-on-tickin/ba-p/2281826</link>
      <description>&lt;P&gt;I got a call the other day from Otzi (he and I go &lt;A href="http://en.wikipedia.org/wiki/Otzi"&gt;&lt;EM&gt;way&lt;/EM&gt; back&lt;/A&gt;). He ran across a piece of code that just didn't make any sense to him:&lt;/P&gt;&lt;PRE style="margin-left: 20px; line-height: 1; color: firebrick;"&gt;&lt;BR /&gt;var ans = undefined;&lt;BR /&gt;try {&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; ans = otzi.process();&lt;BR /&gt;}&lt;BR /&gt;catch (err) {&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; gs.log(err);&lt;BR /&gt;}&lt;BR /&gt;finally {&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; if (ans === undefined)&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ans = null;&lt;BR /&gt;}&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;"What," he demanded, "does that gobbledegook mean?"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Well, Otzi, here's your answer...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;There are three parts to this. First, there's the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;try { }&lt;/SPAN&gt; block. The curly braces enclose the statements we're &lt;EM&gt;trying&lt;/EM&gt;:&lt;/P&gt;&lt;PRE style="margin-left: 20px; line-height: 1; color: firebrick;"&gt;&lt;BR /&gt;try {&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; ans = otzi.process();&lt;BR /&gt;}&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;In Otzi's example, there's only one line inside the curly braces, but there could have been any number of lines. The code inside those curly braces will execute just like it would if it &lt;EM&gt;wasn't&lt;/EM&gt; inside the curly braces, with just one difference: if something goes horribly wrong when executing that code, the next part (below) comes into play. By "horribly wrong," I mean that some kind of error occurs. For example, suppose we tried executing the code above while the variable &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;otzi&lt;/SPAN&gt; was null, or even undefined? Without the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;try { }&lt;/SPAN&gt;, you'd get an error in the log and your script would stop. With the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;try { }&lt;/SPAN&gt; block, something entirely different happens:&lt;/P&gt;&lt;PRE style="margin-left: 20px; line-height: 1; color: firebrick;"&gt;&lt;BR /&gt;catch (err) {&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; gs.log(err);&lt;BR /&gt;}&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;If an error occurs when the code inside the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;try { }&lt;/SPAN&gt; block executes, the code inside the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;catch {err}&lt;/SPAN&gt; block will execute. For example, if &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;otzi&lt;/SPAN&gt; was null when we ran the script, JavaScript would &lt;EM&gt;throw&lt;/EM&gt; an error when it tried to execute the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;.process()&lt;/SPAN&gt; method (it's kind of hard for null to have a &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;.process()&lt;/SPAN&gt; method!). When JavaScript &lt;EM&gt;throws&lt;/EM&gt; an error, it creates an instance of the &lt;A title="eveloper.mozilla.org/en/JavaScript/Reference/Global_Objects/Error" href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error"&gt;Error class&lt;/A&gt; containing a message describing the error. Then it calls the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;catch (err)&lt;/SPAN&gt; block, exactly as if &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;catch()&lt;/SPAN&gt; were a function. The code inside your &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;catch (err)&lt;/SPAN&gt; block can then read the message inside the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;Error&lt;/SPAN&gt; instance and do whatever it needs to do. In Otzi's example, all that the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;catch (err)&lt;/SPAN&gt; block is doing is logging the error — but it could do anything at all.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Finally, there's the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;finally { }&lt;/SPAN&gt; block:&lt;/P&gt;&lt;PRE style="margin-left: 20px; line-height: 1; color: firebrick;"&gt;&lt;BR /&gt;finally {&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; if (ans === undefined)&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ans = null;&lt;BR /&gt;}&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;The first thing to know about the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;finally { }&lt;/SPAN&gt; block is that it's optional — &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;try { }&lt;/SPAN&gt; blocks don't have to be followed by a &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;finally { }&lt;/SPAN&gt; block, and most of the time they aren't. When there &lt;EM&gt;is&lt;/EM&gt; a &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;finally { }&lt;/SPAN&gt; block, the code inside of it will be executed &lt;EM&gt;no matter what happens&lt;/EM&gt; when executing the code inside the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;catch { }&lt;/SPAN&gt; block. In Otzi's example, the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;finally { }&lt;/SPAN&gt; block is simply ensuring that the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;ans&lt;/SPAN&gt; variable ends up defined: even if an error occurs, even if an undefined value is returned.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;finally { }&lt;/SPAN&gt; block finishes executing (or when the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;catch { }&lt;/SPAN&gt; block finishes, if there is no &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;finally { }&lt;/SPAN&gt; block), the script keeps on executing. This is really the most important thing of all about using a &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;try { }/catch { }&lt;/SPAN&gt; construct: it's a way for your code to take a lickin' and keep on tickin'. For any situation where an error &lt;EM&gt;could&lt;/EM&gt; occur, but isn't catastrophic (and therefore doesn't warrant aborting the script's execution), the &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;try { }/catch { }&lt;/SPAN&gt; construct is your friend.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A couple more details worth knowing:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If your script is calling Java code (whether through a Packages.xxx call or through a Java class exposed as a JavaScript class), and that code throws a Java exception (a mechanism within Java that's very much like the JavaScript &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;try { }/catch { }&lt;/SPAN&gt; construct), then JavaScript will turn that Java exception into a JavaScript Error and throw it. Bottom line: you can &lt;EM&gt;catch&lt;/EM&gt; one of these error just like you can catch a JavaScript Error.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;JavaScript doesn't have a monopoly on the ability to throw an error — your own code can do it. Here's a trivial example:&lt;/P&gt;&lt;PRE style="margin-left: 20px; line-height: 1; color: firebrick;"&gt;&lt;BR /&gt;var x = 0;&lt;BR /&gt;try {&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; gs.log(digits(x));&lt;BR /&gt;}&lt;BR /&gt;catch (err) {&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; gs.log(err);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;// return the number of integer digits in a positive number...&lt;BR /&gt;function digits(arg) {&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; if (arg &amp;lt;= 0)&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; throw new Error('Argument must be positive!');&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; return 1 + Math.floor(Math.LOG10E * Math.log(arg));&lt;BR /&gt;}&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;If &lt;SPAN style="font-family=courier;color: FireBrick;"&gt;x&lt;/SPAN&gt; has a number greater than zero, we'll log the number of integer digits in that number. Otherwise our function will throw an error, we &lt;EM&gt;won't&lt;/EM&gt; log the number of digits (which we couldn't compute), and we'll log an error instead.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Are you feeling a little more enlightened now, oh ancient Otzi?&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2011 15:41:23 GMT</pubDate>
      <guid>https://www.servicenow.com/community/in-other-news/take-a-lickin-and-keep-on-tickin/ba-p/2281826</guid>
      <dc:creator>SlightlyLoony</dc:creator>
      <dc:date>2011-11-15T15:41:23Z</dc:date>
    </item>
  </channel>
</rss>

