Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Follow publication

Code Smell 73 — Exceptions for Expected Cases

Maximiliano Contieri
Dev Genius
Published in
2 min readMay 31, 2021

Photo by Greg Rosenke on Unsplash

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. Read the contract.

Sample Code

Wrong

Right

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

More info

https://wiki.c2.com/?DontUseExceptionsForFlowControl

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

Richard Pattis

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Written by Maximiliano Contieri

I’m a senior software engineer specialized in declarative designs. S.O.L.I.D. and agile methodologies fan. Maximilianocontieri.com

No responses yet

Write a response