Code Smell 73 - Exceptions for Expected Cases

Code Smell 73 - Exceptions for Expected Cases

Maxi Contieri
·May 31, 2021·

1 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Exceptions are handy Gotos and flags. Let's abuse them.

TL;DR: Do not use exceptions for flow control.


  • Readability

  • Principle of least astonishment Violation.


  1. Use Exceptions just for unexpected situations.

  2. Exceptions handle contract violations. Read the contract.

Sample Code


try {
    for (int i = 0;; i++)
    } catch (ArrayIndexOutOfBoundsException e) {}

//Endless loop without end condition
for (int index = 0; index < array.length; index++)

//index < array.length breaks execution


This is a semantic smell. Unless we use machine learning linters it will be very difficult to find the mistakes.


  • Readability


Exceptions are handy, and we should definitively use them instead of returning codes.

The boundary between correct usage and wrong usage is blur like so many design principles.


More info


Photo by Greg Rosenke on Unsplash

When debugging, novices insert corrective code; experts remove defective code.

Richard Pattis

This article is part of the CodeSmell Series.

Share this


Technical Opinions are my own. I don't have the revealed truth.

Software Design is a creative activity. These are hints and not rigid rules.

I write on BackEnd Business Systems and OOP Design. My advice/experience might not suit other systems.

You can write me at info(at)