# 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.

# Problems

- Readability

- Principle of least astonishment Violation.

# Solutions

1. Use Exceptions just for unexpected situations.

2. Exceptions handle [contract violations](https://en.wikipedia.org/wiki/Design_by_contract). Read the contract.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/14fe90a45804c47d898bab4fe8d17d36)
```java
try {
	for (int i = 0;; i++)
		array[i]++;
	} catch (ArrayIndexOutOfBoundsException e) {}

//Endless loop without end condition
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/49811ad62691011166020c49c1c2ed71)
```java
for (int index = 0; index < array.length; index++)
		array[index]++;

//index < array.length breaks execution
```

# Detection

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

# Tags

- Readability

# Conclusion

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.

# Relations

%[https://maximilianocontieri.com/code-smell-72-return-codes]

%[https://maximilianocontieri.com/code-smell-26-exceptions-polluting]

# More info

- [Don't use exceptions for flow control](https://wiki.c2.com/?DontUseExceptionsForFlowControl)

- [Java Zone](https://dzone.com/articles/exceptions-as-controlflow-in-java)

- [Stack Exchange](https://softwareengineering.stackexchange.com/questions/189222/are-exceptions-as-control-flow-considered-a-serious-antipattern-if-so-why)

- [Stack Overflow](https://stackoverflow.com/questions/729379/why-not-use-exceptions-as-regular-flow-of-control)

# Credits

Photo by <a href="https://unsplash.com/@greg_rosenke">Greg Rosenke</a> on <a href="https://unsplash.com/s/photos/bounds">Unsplash</a>
  

* * *

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

_Richard Pattis_
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
