paint-brush
When You Forget to Throw, Your Code Will Blow โ€‚by@mcsee
147 reads

When You Forget to Throw, Your Code Will Blow

by Maximiliano ContieriFebruary 3rd, 2025
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Creating a new exception without throwing it leads to silent failures.
featured image - When You Forget to Throw, Your Code Will Blow
Maximiliano Contieri HackerNoon profile picture
0-item
1-item


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 ๐Ÿ˜ƒ

  1. Always ensure you throw exceptions
  2. Check exception usage and catching
  3. Test exception paths
  4. Use linters
  5. 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


This article is part of the CodeSmell Series.