Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 24, 2022 02:02 pm GMT

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

# badimport loggingdef send_email():   print("Sending email")   raise ConnectionError("Oops")try:  send_email() except:   # AVOID THISpass

Right

import logginglogger logging.getLogger(__name___)try:  send_email()except ConnectionError as exc:  logger.error(f"Cannot send email {exc}")

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.


Original Link: https://dev.to/mcsee/code-smell-165-empty-exception-blocks-27bg

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To