This content originally appeared on HackerNoon and was authored by Maximiliano Contieri
\
TL;DR: Creating a new exception without throwing it leads to silent failures.
\ When You Forget to Throw, Your Code Will Blow 💣💥
Problems 😔
- Silent failures
- Unhandled errors
- Misleading logic
- Hidden defects
- Hollow Exceptions
Solutions 😃
- Always ensure you throw exceptions
- Check exception usage and catching
- Test exception paths
- Use linters
- Avoid creating unused exceptions
Context 💬
When you create a new exception but forget to throw it, your code might appear to work correctly, but it silently ignores critical errors.
\ Creating exceptions is the same as creating business objects and constructors should not have side effects.
\ Unless you throw them, it is dead code.
Sample Code
Wrong 🚫
class KlendathuInvasionError(Exception):
def __init__(self, message):
self.message = message
# This is a hollow exception
def deploy_troops(safe):
if not safe:
KlendathuInvasionError("Drop zone is hot!")
# Never thrown
print("Troopers deployed.")
deploy_troops(False)
Right 👉
class KlendathuInvasionError(Exception):
def __init__(self, message):
super().__init__(message)
def deploy_troops(safe):
if not safe:
raise Exception("Drop zone is hot!")
# You throw the exception
print("Troopers deployed.")
try:
deploy_troops(False)
except KlendathuInvasionError as e:
print(f"Abort mission: {e}")
# You handle the exception
Detection 🔍
You can detect this smell by reviewing your code for instances where you create exceptions but do not raise them.
\ You can also search for instances where an exception is instantiated but never raised.
\ Automated linters and static analyzers can flag such issues.
Tags 🏷️
- Exceptions
Level 🔋
- Beginner
Why the Bijection Is Important 🗺️
An exception represents a real-world failure inside your program.
\ If you create an exception but never throw it, your code lies about the presence of an error.
\ When you fail to throw an exception, you break the one-to-one correspondence between the real-world problem and its coding representation.
AI Generation 🤖
AI generators might create this smell if they generate exception-handling code without ensuring that exceptions are properly raised.
\ Always review AI-generated code for proper error handling.
AI Detection 🦾
AI-based linters can detect this smell by analyzing unreferenced exception instances.
\ Fixing it requires proper domain knowledge to decide where to throw the exception.
Try Them! 🛞
Remember: AI Assistants make lots of mistakes
| Without Proper Instructions | With Specific Instructions | |----|----| | ChatGPT | ChatGPT | | Claude | Claude | | Perplexity | Perplexity | | Copilot | Copilot | | Gemini | Gemini | | DeepSeek | DeepSeek | | Meta AI | Meta AI |
Conclusion ✔️
Always throw exceptions immediately after you create them.
\ Silent failures can lead to significant issues in your code, making it harder to maintain and debug.
\ Proper error handling and good coverage ensure your code behaves predictably and reliably.
Relations 👩❤️💋👨
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxxiii
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-vi-cmj31om
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxix
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-ii-o96s3wl4
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxvii
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xv
Disclaimer 📘
Code Smells are my opinion.
Credits 🙏
Photo by Bethany Reeves on Unsplash
Software is like entropy: It is difficult to grasp, weighs nothing, and obeys the Second Law of Thermodynamics; i.e., it always increases.
Norman Augustine
https://hackernoon.com/400-thought-provoking-software-engineering-quotes?embedable=true
This article is part of the CodeSmell Series.
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-i-xqz3evd?embedable=true
\
This content originally appeared on HackerNoon and was authored by Maximiliano Contieri
Maximiliano Contieri | Sciencx (2025-02-03T16:25:24+00:00) When You Forget to Throw, Your Code Will Blow. Retrieved from https://www.scien.cx/2025/02/03/when-you-forget-to-throw-your-code-will-blow/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.