- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
CompositeThrowable inconsistency? (parallel collections processing)
Tue, 2012-01-24, 22:23
I have this small program (also available at https://gist.github.com/1672660)
;
object Test {
def main(args: Array[String]) {
def process(animal: String, n: Int) = {
if (n % 13 == 0 && n > 0)
throw new Exception("My " + animal + " hates number " + n)
n
}
def show(what: String, messages: Seq[String]) {
println(what)
messages.foreach(m => println(" - " + m))
}
def test(u: () => Any) {
println
try {
u()
} catch {
case e: scala.collection.parallel.CompositeThrowable =>
show("Composite", e.throwables.toList.map(t =>
t.getClass.getName + ": " + t.getMessage))
case e =>
show("Simple", Seq(e.getMessage))
}
}
test(() => (0 until 20).map(process("hamster", _)))
test(() => (0 until 20).par.map(process("hamster", _)))
test(() => (0 until 50).map(process("rabbit", _)))
test(() => (0 until 50).par.map(process("rabbit", _)))
test(() => List("cat", "dog").map(a => (0 until 50).map(process(a,
_))))
test(() => List("cat", "dog").map(a => (0 until
50).par.map(process(a, _))))
test(() => List("cat", "dog").par.map(a => (0 until
50).map(process(a, _))))
test(() => List("cat", "dog").par.map(a => (0 until
50).par.map(process(a, _))))
}
}
When I run it, I get these results:
$ scala composite.scala
Simple
- My hamster hates number 13
Simple
- My hamster hates number 13
Simple
- My rabbit hates number 13
Composite
- java.lang.Exception: My rabbit hates number 26
- java.lang.Exception: My rabbit hates number 39
- java.lang.Exception: My rabbit hates number 13
Simple
- My cat hates number 13
Composite
- java.lang.Exception: My cat hates number 26
- java.lang.Exception: My cat hates number 39
- java.lang.Exception: My cat hates number 13
Composite
- java.lang.Exception: My cat hates number 13
- java.lang.Exception: My dog hates number 13
Composite
- java.lang.Exception: My cat hates number 13
- java.lang.Exception: My cat hates number 26
In the last test case, I would have expected the reported composite
exception to contain *two other composite exceptions*, rather than two
simple exceptions. With the current behavior, the exception processing
under parallel processing seems inconsistent.
Is this a bug or a design decision?
Many thanks!
JL