Short Circuits, Bottom Types, and the Vacuous Boomerang

It’s a real program, I promise. It compiles and runs. Try it!

It works because throw return is an expression of type Nothing. Normally, an expression is a piece of code that produces a value. When you write something like val x = 1 + 1, you’re saying “evaluate the expression on the right, and then assign the result to the variable on the left.” But when an expression’s return type is Nothing, it means the expression never completes its execution at all. Instead, the program exits that line of code entirely, and skips ahead to somewhere else in the program.

The throw keyword is a good example of an instruction that can cause a program to skip ahead. When you throw an exception, the program will jump right to the next applicable catch or finally section. Or, if the error isn’t caught, the program or thread will crash. Either way, the lines of code after the throw won’t get executed.

Click Here