I recently came across the concept of Try/Success/Failure, and I am wondering how to use it for a method that has the return type Unit. Is using Try[Unit] the correct way? Maybe I am too influenced from my Java background, but is it a good idea to force the caller to deal with the problem?
1 Answer
Try[Unit] is normal. For example, if you persist the entity, you can use:
try { em.persist(entity) } catch{ case ex:PersistenceException => handle(ex) } or just
Try(em.persist(entity)) match { case Success(_) => case Failure(ex) => handle(ex) } 1 Comment
Karda
Oh, I did not know I can also use a catch at this point. Thanks.
Option[MyException]withNonebeing the default case, andSome(exception)being the result in case of errors?Try[Unit]is perfectly fine.Try[String]and another method takingStringand returningOption[Exception], they are harder to use together than if the second method returnsTry[Unit].