#Scala Try error handling without StackTraces

After reading this excellent Blogpost about the new Scala Try, i was thinking of how this would come in handy for "flow-control", but without the StackTrace overhead.

The Problem

class FlowException(msg: String) extends Exception(msg)
val myE = new FlowException("foo")

Since throw does not generate the StackTrace, but new does, we simply extend it with NoStackTrace!

class FlowException(msg: String) extends Exception(msg) with util.control.NoStackTrace

Now we have an Exception-class which does not contain an expensive StackTrace since we don't need to know where, but if an error has happened.

Why do we need it?

In our netflow.io project, we need to be able to know "upstream" when there is something wrong, but using Either is imho very ugly.

Returning the case class Failure with a new StackTrace-less exception is very cool, especially since now your "real" exceptions thrown by Java libraries will be handled as usual and you can filter the Exception for being a subclass of your own (e.g. FlowException).

This way we know that a Failure(e: FlowException) is a "known" or expected exception and thus does not need any further investigation.

Upstream you can do whatever you want with that information, in our case, we simply let the akka Actor Logger.warn/error that exception without running .printStackTrace.

In my Opinion

this is a really good thing, i might be wrong due to a lack of in-depth JVM knowledge, but i think the reasoning is sound. ;)

Flattr me!

Tell your friends!