In Scala, is there a pre-existing library function for converting exceptions to Options?

前端 未结 5 1389
心在旅途
心在旅途 2021-02-05 06:32

This is basically to wrap java factory methods which throw exceptions if the item can\'t be created based on the inputs. I\'m looking for something in the base library like:

5条回答
  •  醉梦人生
    2021-02-05 07:03

    Scalaz provides Validation[+E, +A] which is similar to Either.

    val result: Validation[Throwable, Something] = ...
    
    result match {
      case Success(x) => ...
      case Failure(x) => ...
    }
    

提交回复
热议问题