简体   繁体   中英

Return type from Try in scala

I am trying to count number of records, and if i get them return the records else just log the error.

Following is the snippet:


    val records: Any = Try(count()) match {
                case Success(records) => records
                case Failure(exception) => logger.error(s"Exception occurred")
              }

The data type of count method is Int but because of wrapping it in Try, getting type as any. How can i solve the problem?

what is a return type of count method / field returns. If you want just ignore the exception and print it then continue, you should have done something like this:


  val records: List[Int] = Try(count()) match {
    case Success(records) => records
    case Failure(exception) =>
      log.error("Exception occurred", exception)
      List.empty
  }

  def count(): List[Int] = List(1, 2 ,3)

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