Try and getOrElse scala
<pre><code>val label = Try("here_a").getOrElse("here_b")
</code></pre>

<p>In the case where <code>here_a</code> is not found, this is not falling back to <code>here_b</code>. Why is the <code>.getOrElse</code> not functioning?</p>

<p>Thanks @jwvh. These values are sting files paths and thus the exception is the following <code>Exception in thread "main" java.io.FileNotFoundException:</code> </p>

<p>As per Andrew James Ramirez's comment I tried this but issue persists.</p>

<p><code>Try(throw new Exception("FAIL here_a")).getOrElse("here_b")</code></p>

<p>I have also tried</p>

<p><code>Try(throw new Exception("FileNotFoundException here_a")).getOrElse("here_b")</code></p>

<p><em>Edit</em></p>

<p>It seems I may have oversimplified this question for SO. Some more context. The string is actually a file path. Perhaps this is making a difference?</p>

<p>Effectively, a <code>json</code> file may be found in one of two possible locations. I thus wish to try the first location and if a <code>java.io.FileNotFoundException</code> is returned, fall back on the second location. This is what I presently have:</p>

<p><code>val input_file = Try(throw new Exception("FAIL location_a/file_a.json")).getOrElse("location_b/file_a.json")</code></p>

<p><em>Edit</em> V2</p>

<p>I am embarrassed to say that I found the simple error. I am running this scala code on <code>spark</code> and I forgot to repackage in between testing. <code>sbt package</code> was all that was required. :-/</p>