Q:

Handle exception for Scala Async and Future variable, error accessing variable name outside try block

@scala.throws[scala.Exception]  
def processQuery(searchQuery : scala.Predef.String) : scala.concurrent.Future[io.circe.Json] = { /* compiled code */ }

How do I declare the searchResult variable at line 3 so that it can be initailized inside the try block and can be processed if it's successful after and outside the try block. Or, is there any other way to handle the exception? The file containing processQuery function is not editable to me, it's read-only.
def index = Action.async{ implicit request =>
  val query = request.body.asText.get
  var searchResult : scala.concurrent.Future[io.circe.Json] = Future[io.circe.Json]  //line 3
  var jsonVal = ""
  try {
    searchResult = search.processQuery(query) 
  } catch {
    case e :Throwable =>  jsonVal = e.getMessage
  }

  searchResult onSuccess ({
    case result => jsonVal = result.toString()
  })

  searchResult.map{ result =>
    Ok(Json.parse(jsonVal))
  }    
}

if declared in the way it has been declared it's showing compilation error

A:

Would using the recover method help you? I also suggest to avoid var and use a more functional approach if possible. In my world (and play Json library), I would hope to get to something like:
def index = Action.async { implicit request =>
  processQuery(request.body.asText.get).map { json =>
    Ok(Json.obj("success" -> true, "result" -> json))
  }.recover {
    case e: Throwable => Ok(Json.obj("success" -> false, "message" -> e.getMessage))
  }
}

I guess it may be necessary to put the code in another whole try catch:
try {
  processQuery....
  ...
} catch { 
  ...
}

