diff options
author | Lukas Rytz <lukas.rytz@typesafe.com> | 2016-04-01 13:38:11 +0200 |
---|---|---|
committer | Lukas Rytz <lukas.rytz@typesafe.com> | 2016-04-01 13:38:11 +0200 |
commit | fe99c881edf127b9082fe15565ccfddb925f6fd4 (patch) | |
tree | 02c8401d758a9f2ab619bd981ead83215eac4cbd /src | |
parent | 96f230a0e9c254c45dc91c71b5929639e6add1f0 (diff) | |
parent | 20d262c0bb6615708ce95c399f9f842b4e93bb3c (diff) | |
download | scala-fe99c881edf127b9082fe15565ccfddb925f6fd4.tar.gz scala-fe99c881edf127b9082fe15565ccfddb925f6fd4.tar.bz2 scala-fe99c881edf127b9082fe15565ccfddb925f6fd4.zip |
Merge pull request #5071 from janekdb/topic/2.12.x-scaladoc-Exceptions
Add initial unit test for Catch and augment documentation
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/util/control/Exception.scala | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/src/library/scala/util/control/Exception.scala b/src/library/scala/util/control/Exception.scala index 702d3b5e5e..8aa4073a51 100644 --- a/src/library/scala/util/control/Exception.scala +++ b/src/library/scala/util/control/Exception.scala @@ -83,6 +83,10 @@ object Exception { * Pass a different value for rethrow if you want to probably * unwisely allow catching control exceptions and other throwables * which the rest of the world may expect to get through. + * @tparam T result type of bodies used in try and catch blocks + * @param pf Partial function used when applying catch logic to determine result value + * @param fin Finally logic which if defined will be invoked after catch logic + * @param rethrow Predicate on throwables determining when to rethrow a caught [[Throwable]] */ class Catch[+T]( val pf: Catcher[T], @@ -105,10 +109,12 @@ object Exception { } finally fin foreach (_.invoke()) - /* Create an empty Try container with this Catch and the supplied `Finally`. */ - def andFinally(body: => Unit): Catch[T] = fin match { - case None => new Catch(pf, Some(new Finally(body)), rethrow) - case Some(f) => new Catch(pf, Some(f and body), rethrow) + /** Create a new Catch container from this object and the supplied finally body. + * @param body The additional logic to apply after all existing finally bodies + */ + def andFinally(body: => Unit): Catch[T] = { + val appendedFin = fin map(_ and body) getOrElse new Finally(body) + new Catch(pf, Some(appendedFin), rethrow) } /** Apply this catch logic to the supplied body, mapping the result @@ -117,13 +123,13 @@ object Exception { def opt[U >: T](body: => U): Option[U] = toOption(Some(body)) /** Apply this catch logic to the supplied body, mapping the result - * into Either[Throwable, T] - Left(exception) if an exception was caught, - * Right(T) otherwise. + * into `Either[Throwable, T]` - `Left(exception)` if an exception was caught, + * `Right(T)` otherwise. */ def either[U >: T](body: => U): Either[Throwable, U] = toEither(Right(body)) /** Apply this catch logic to the supplied body, mapping the result - * into Try[T] - Failure if an exception was caught, Success(T) otherwise. + * into `Try[T]` - `Failure` if an exception was caught, `Success(T)` otherwise. */ def withTry[U >: T](body: => U): scala.util.Try[U] = toTry(Success(body)) |