Checked Exceptions?

Mark "Justin" Waks
1 min readMar 12, 2021

Just when I think the Dotty project is completely settling down into staid boring bugfixes, a new experiment comes along.

Let’s note upfront: even more than usual, this is super-duper experimental — just a draft PR, far from merged, much less being seriously discussed for inclusion in Scala 3. I suspect that, at the earliest, we might wind up talking about something like this in stdlib for 3.1.

That said, check out this fascinating PR from Martin. It is basically kicking the tires with a way that you could implement typesafe checked Exceptions in Scala, with no runtime overhead, with only a little new compiler support beyond stuff that’s already in.

The most interesting bit is the tests, of which there are a bunch, demonstrating how you could, using standard language machinery, define an erased given CanThrow type and an infix throws type, allowing you to say things like

def bar: Int throws Exception =
raise(Fail())

The idea of checked Exceptions is a bit controversial, given its history in Java, but this is pretty neat to see, not least as an illustration of how much power you can derive from the tools already in the language, without having to bake everything into the compiler.

I commend it as worth a read, for educational purposes if nothing else — it’s a great demonstration of the power of the new erased keyword, and the notion of capabilities that is starting to emerge.

--

--

Mark "Justin" Waks

Lifelong programmer and software architect, specializing in online social tools and (nowadays) Scala. Architect of Querki (“leading the small data revolution”).