title: Javascript try...catch...else...finally like Python, Java, Ruby, etc
body: <p>How can Javascript duplicate the four-part <code>try</code>-<code>catch</code>-<code>else</code>-<code>finally</code> execution model that other languages support?</p>
  
  <p>A clear, brief summary is from the <a href="http://docs.python.org/whatsnew/2.5.html#pep-341-unified-try-except-finally" rel="noreferrer">Python 2.5 what's new</a>. In Javascript terms:</p>
  
  <pre><code>// XXX THIS EXAMPLE IS A SYNTAX ERROR
  try {
    // Protected-block
  } catch(e) {
    // Handler-block
  } else {
    // Else-block
  } finally {
    // Final-block
  }
  </code></pre>
  
  <p>The code in <strong>Protected-block</strong> is executed. If the code throws an exception, <strong>Handler-block</strong> is executed; If no exception is thrown, <strong>Else-block</strong> is executed.</p>
  
  <p>No matter what happened previously, <strong>Final-block</strong> is executed once the code block is complete and any thrown exceptions handled. Even if there’s an error in <strong>Handler-block</strong> or <strong>Else-block</strong> and a new exception is raised, the code in <strong>Final-block</strong> is still run.</p>
  
  <p>Note that cutting <strong>Else-block</strong> and pasting at the end of <strong>Protected-block</strong> is <em>wrong</em>. If an error happens in <strong>Else-block</strong>, it <em>must not</em> be handled by <strong>Handler-block</strong>.</p>