简体   繁体   中英

How do I write this without using a Try/Catch block?

I am looking to rewrite this scala function, but I am new to the language, I understand there is a alternative to using try\\catch blocks. How would you guys rewrite this function?

  def updateStationPost = Action { implicit request =>
    StationForm.bindFromRequest.fold(
      errors => { //needs to be revised!!
        BadRequest(html.updateStation(errors,
             Station(
              request.body.asFormUrlEncoded.get("id")(0).toLong,
              request.body.asFormUrlEncoded.get("operator")(0).toLong,
              request.body.asFormUrlEncoded.get("name")(0),
              try {
                request.body.asFormUrlEncoded.get("number")(0).toInt
              } catch {
                case e:Exception => { 0 } //this exception happens when trying to convert the number when there is nothing in the flash scope to convert.
              },
              request.body.asFormUrlEncoded.get("timezone")(0)
            ),
            Operators.retrieveJustOperators() //ugh... needs to be revised..
          )
        )
      },
      { case(stationFormObj) =>
        Stations.update(stationFormObj)
        Redirect(routes.StationsController.index)
      }
    )
  }

A general way of managing this is to use Try to wrap code that could throw an exception. Some of the ways of using this are illustrated below:

def unpredictable() = {
  Try(Console.readLine("Int please: ").toInt) getOrElse 0
}

If the console read does not contain a parseable integer, then it throws an exception. This code just returns a 0 if there was an error, but you could put other statements there. As an alternative, you could use pattern matching to handle the situation.

def unpredictable() =  {
  Try(Console.readLine("Int please: ").toInt) match {
    case Success(i) => i
    case Failure(e) => println(e.getMessage()) 
  }
}

You can also just return a Try and let the caller decide how to handle the failure.

How about:

import scala.util.control.Exception.handling

// Create a val like this as you reuse it over and over
val form: Option[Map[String, Seq[String]]] = request.body.asFormUrlEncoded

// Create some helper functions like this
val nfeHandler = handling(classOf[NumberFormatException]) by (_ => 0)
val intNFEHandler = (str: String) => nfeHandler apply str.toInt
val longNFEHandler = (str: String) => nfeHandler apply str.toLong

// You can use this instead of your try catch.. but this is just a sugar.. perhaps cleaner
intNFEHandler apply form.get("id")(0)

Here if the form was something like: Option(Map("id" -> Seq.empty[String]))

form.get("id")(0) would blow up with java.lang.IndexOutOfBoundsException.

I would suggest to have another helper:

// takes fieldNames and returns Option(fieldValue)
val fieldValueOpt = (fieldName: String) => form.flatMap(_.get(fieldName).flatMap(_.headOption))

Then create a validate method which performs pattern matching on all the fieldValue optionals, extract the values and create your Station object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM