DEV Community

Cover image for Code Smell 73 - Exceptions for Expected Cases
Maxi Contieri ⭐⭐⭐
Maxi Contieri ⭐⭐⭐

Posted on • Originally published at


Code Smell 73 - Exceptions for Expected Cases

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
Enter fullscreen mode Exit fullscreen mode


for (int index = 0; index < array.length; index++)

//index < array.length breaks execution
Enter fullscreen mode Exit fullscreen mode


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.

Top comments (0)

16 Libraries You Should Know as a React Developer

Being a modern React developer is not about knowing just React itself. To stay competitive, it is highly recommended to explore the whole ecosystem. This article contains some of the most useful React component libraries to speed up your developer workflow.