This content originally appeared on DEV Community 👩💻👨💻 and was authored by Maxi Contieri
On Error resume next was the first thing I learned in my first job
TL;DR: Don't avoid exceptions. Handle Them.
Problems
- Fail Fast Principle Violation
Solutions
- 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
Right
import logging
logger 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
Code Smell 132 - Exception Try Too Broad
Maxi Contieri ・ May 18 ・ 2 min read
More Info
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
Software Engineering Great Quotes
Maxi Contieri ・ Dec 28 '20 ・ 13 min read
This article is part of the CodeSmell Series.
How to Find the Stinky parts of your Code
Maxi Contieri ・ May 21 '21 ・ 7 min read
This content originally appeared on DEV Community 👩💻👨💻 and was authored by Maxi Contieri
Maxi Contieri | Sciencx (2022-09-24T14:02:05+00:00) Code Smell 165 – Empty Exception Blocks. Retrieved from https://www.scien.cx/2022/09/24/code-smell-165-empty-exception-blocks/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.