DEV Community

Cover image for Code Smell 165 - Empty Exception Blocks
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

5

Code Smell 165 - Empty Exception Blocks

On Error resume next was the first thing I learned in my first job

TL;DR: Don't avoid exceptions. Handle Them.

Problems

Solutions

  1. Catch the exception and deal with it explicitly

Context

On early programming days, we privileged the systems running before error handling.

We have evolved.

Sample Code

Wrong

# bad
import logging

def send_email(): 
  print("Sending email") 
  raise ConnectionError("Oops")

try:
  send_email() 
except: 
  # AVOID THIS
pass
Enter fullscreen mode Exit fullscreen mode

Right

import logging

logger logging.getLogger(__name___)
try:
  send_email()
except ConnectionError as exc:
  logger.error(f"Cannot send email {exc}")
Enter fullscreen mode Exit fullscreen mode

Detection

[X] Automatic

Many linters warn us on empty exception blocks

Exceptions

If we need to skip and ignore the exception, we should document it explicitly.

Tags

  • Exceptions

Conclusion

Prepare to deal with the errors.

Even if you decide to do nothing, you should be explicit with this decision.

Relations

More Info

On Error Resume Next Package

Disclaimer

Code Smells are just my opinion.

Credits

Photo by James Best on Unsplash

Thank you @Jan Giacomelli


Optimization hinders evolution. Everything should be built top-down, except the first time. Simplicity does not precede complexity, but follows it.

Alan Perlis


This article is part of the CodeSmell Series.

Runner H image

Tame the Chaos of Slack, Notion, Discord & More

Too many tools, too little time? Let Runner H’s AI agent handle your messages, notes, syncs, and checklists — across your entire stack.

Try for Free

Top comments (0)

Build gen AI apps that run anywhere with MongoDB Atlas

Build gen AI apps that run anywhere with MongoDB Atlas

MongoDB Atlas bundles vector search and a flexible document model so developers can build, scale, and run gen AI apps without juggling multiple databases. From LLM to semantic search, Atlas streamlines AI architecture. Start free today.

Start Free

👋 Kindness is contagious

Discover this thought-provoking article in the thriving DEV Community. Developers of every background are encouraged to jump in, share expertise, and uplift our collective knowledge.

A simple "thank you" can make someone's day—drop your kudos in the comments!

On DEV, spreading insights lights the path forward and bonds us. If you appreciated this write-up, a brief note of appreciation to the author speaks volumes.

Get Started