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:
def exceptionToOption[A](f: => A):Option[A] ={
try{
Some(f)}
catch{
case e:Exception => None}
}
Usage:
val id:Option[UUID] = exceptionToOption(UUID.fromString("this will produce None"))
I know I can write my own but I want to check I am not re-inventing the wheel.
Use scala.util.control.Exception:
And you can make it more sophisticated. For example, to catch only arithmetic exceptions and retrieve the exception: