title: Scala: exception handling in anonymous function
body: <p>If I pass an anonymous function as an argument, like e.g. in this code sample:</p>
  
  <pre><code>val someMap = someData.map(line =&gt; (line.split("\\|")(0), // key 
                                      line.split("\\|")(1) + "|" + // value as string concat
                                      line.split("\\|")(4) + "|" +
                                      line.split("\\|")(9)))
  </code></pre>
  
  <p>I could catch, e.g. an ArrayIndexOutOfBoundsException like this:</p>
  
  <pre><code>try {
      val someMap = someData.map(line =&gt; (line.split("\\|")(0), // key 
                                          line.split("\\|")(1) + "|" + // value as string concat
                                          line.split("\\|")(4) + "|" +
                                          line.split("\\|")(9)))
  } catch {
      case e1: ArrayIndexOutOfBoundsException =&gt; println("exception in line " )
  }
  </code></pre>
  
  <p>The problem with this is that I do not have access to the inner function's scope. In this case I would like to print the <code>line</code> (from the anonymous function) which caused the exception.</p>
  
  <p>How can I do this? Is there some way of catching an exception within an anonymous function? Is there a way to access the scope of an anonymous function from the outside for debugging purposes?</p>
  
  <p><strong>edit:</strong> I'm using Scala 2.9.3</p>