Skip to content

Commit 20d262c

Browse files
committed
Add initial unit test for Catch and augment documentation
- Add unit test for andFinally - Reduce code duplication in andFinally - Extend documentation
1 parent 4fc7d55 commit 20d262c

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

src/library/scala/util/control/Exception.scala

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ object Exception {
8383
* Pass a different value for rethrow if you want to probably
8484
* unwisely allow catching control exceptions and other throwables
8585
* which the rest of the world may expect to get through.
86+
* @tparam T result type of bodies used in try and catch blocks
87+
* @param pf Partial function used when applying catch logic to determine result value
88+
* @param fin Finally logic which if defined will be invoked after catch logic
89+
* @param rethrow Predicate on throwables determining when to rethrow a caught [[Throwable]]
8690
*/
8791
class Catch[+T](
8892
val pf: Catcher[T],
@@ -105,10 +109,12 @@ object Exception {
105109
}
106110
finally fin foreach (_.invoke())
107111

108-
/* Create an empty Try container with this Catch and the supplied `Finally`. */
109-
def andFinally(body: => Unit): Catch[T] = fin match {
110-
case None => new Catch(pf, Some(new Finally(body)), rethrow)
111-
case Some(f) => new Catch(pf, Some(f and body), rethrow)
112+
/** Create a new Catch container from this object and the supplied finally body.
113+
* @param body The additional logic to apply after all existing finally bodies
114+
*/
115+
def andFinally(body: => Unit): Catch[T] = {
116+
val appendedFin = fin map(_ and body) getOrElse new Finally(body)
117+
new Catch(pf, Some(appendedFin), rethrow)
112118
}
113119

114120
/** Apply this catch logic to the supplied body, mapping the result
@@ -117,13 +123,13 @@ object Exception {
117123
def opt[U >: T](body: => U): Option[U] = toOption(Some(body))
118124

119125
/** Apply this catch logic to the supplied body, mapping the result
120-
* into Either[Throwable, T] - Left(exception) if an exception was caught,
121-
* Right(T) otherwise.
126+
* into `Either[Throwable, T]` - `Left(exception)` if an exception was caught,
127+
* `Right(T)` otherwise.
122128
*/
123129
def either[U >: T](body: => U): Either[Throwable, U] = toEither(Right(body))
124130

125131
/** Apply this catch logic to the supplied body, mapping the result
126-
* into Try[T] - Failure if an exception was caught, Success(T) otherwise.
132+
* into `Try[T]` - `Failure` if an exception was caught, `Success(T)` otherwise.
127133
*/
128134
def withTry[U >: T](body: => U): scala.util.Try[U] = toTry(Success(body))
129135

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* __ *\
2+
** ________ ___ / / ___ Scala API **
3+
** / __/ __// _ | / / / _ | (c) 2016-2016, LAMP/EPFL **
4+
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
5+
** /____/\___/_/ |_/____/_/ | | **
6+
** |/ **
7+
\* */
8+
9+
package scala.util
10+
11+
import org.junit.runner.RunWith
12+
import org.junit.runners.JUnit4
13+
import org.junit.Test
14+
import org.junit.Assert._
15+
16+
import scala.collection.mutable.ListBuffer
17+
18+
import scala.util.control.Exception._
19+
20+
@RunWith(classOf[JUnit4])
21+
class ExceptionTest {
22+
23+
@Test
24+
def andFinally(): Unit = {
25+
26+
locally {
27+
val audit = ListBuffer[Int]()
28+
val katch = nonFatalCatch[Unit].andFinally(audit append 1)
29+
val result = katch(10)
30+
assertEquals(result, 10)
31+
assertEquals(audit.toList, 1 :: Nil)
32+
}
33+
34+
locally {
35+
val audit = ListBuffer[Int]()
36+
val katch = nonFatalCatch[Unit].andFinally(audit append 1).andFinally(audit append 2)
37+
val result = katch(20)
38+
assertEquals(result, 20)
39+
assertEquals(audit.toList, 1 :: 2 :: Nil)
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)