Why You Should Avoid Using Exceptions as the Control Flow in Java

Learn more about using exceptions as control flow in Java.

Java is a general-purpose programming language with many alternatives to solving a certain problem. However, there are best practices that need to be followed, and there are some bad practices out there as well that are still commonly used.

One of these common bad practices is using exceptions as the control flow. This should be avoided for two reasons: It reduces the performance of your code as a response per unit time, and it makes your code less readable.

Optimized Hierarchy Traverser

My first draft of hierarchy traversing component got some serious feedback, and it's time to make some changes before moving on to the next challenges. Hierarchy traverser is not optimal yet as it uses tail-call recursion and it's easy to run to Stack Overflow with it. This blog post solves this problem and prepares for the next challenges like node cache and continue-from-given-node.

Problem: Running to Stack Overflow

As reader Ants pointed out then, .NET runtime doesn't always optimize tail-call recursion and using this simple piece of code, it's possible to run to Stack Overflow fast.