title: Using try-catch to retrieve the value of nested property. Is it an valid approach?
body: <p>We have an object (referenced by <code>data</code>) and we want to retrieve the value of a nested property. Ideally, we would like to do it like so:</p>
  
  <pre><code>value = data.category3.section2.article4.title;
  </code></pre>
  
  <p>We cannot do this like so, because the above line throws a reference error if any of the mediate objects (<code>category3</code>, <code>section2</code>, or <code>article4</code>) are not defined (at the corresponding positions) inside the <code>data</code> object. </p>
  
  <hr>
  
  <p>Now, to nullify any potential reference errors that might be thrown, we could just place the above line inside a <code>try-catch</code> statement:</p>
  
  <pre><code>try {
      value = data.category3.section2.article4.title;
  } catch (err ) {}
  </code></pre>
  
  <p>This works! However, I am not confident that relying on <code>try-catch</code> in such a way is a good practice. The alternative solution would be to manually traverse to the desired property value. I have written a compact utility function that accomplishes that:</p>
  
  <pre><code>function get( val, names ) {
      names = names.split( '.' );    
      while ( val &amp;&amp; names.length ) { val = val[ names.shift() ]; }    
      return val;
  }
  </code></pre>
  
  <p>Now we can get the property value like so</p>
  
  <pre><code>value = get( data, 'category3.section2.article4.title' );
  </code></pre>
  
  <hr>
  
  <p>So, my question is:</p>
  
  <p><strong>Is the <code>try-catch</code> approach a valid solution?</strong> Or are there valid reasons why it should be avoided? </p>
  
  <p>Btw, the <code>try-catch</code> approach is heavily biased in this thread: <a href="https://stackoverflow.com/questions/6927242/whats-the-simplest-approach-to-check-existence-of-deeply-nested-object-property">What&#39;s the simplest approach to check existence of deeply-nested object property in JavaScript?</a></p>