← Back to list

Clean Code (Error Handling)

Alright I’m back to writing folks.

Elliot. · 2025-09-13 23:49 · 21 claps · 4.6 min read
#clean-code #code #js #programming #error-handling
Open on Medium ↗
Wiki topics: 💻 · Programming

Clean Code (Error Handling)

Alright I’m back to writing folks.

In this article i wanna write about session 7 of clean code books about Error Handling I’m so exciting about it. So let’s get to it ASAP!.

Introduction

So, for the intro part, I want to mention that everyone knows we always have errors in our codebase.

But I don’t want to focus on that “please avoid general error handling in your code”. I want to talk about it carefully, and implement it in a clean way. I know you can do it my friends, everyone can do it! It just requires patience, calmness, and coming with me to describe all the ways you can handle errors more appropriately in this article.

So, let’s go for it!

Use Exceptions Rather Then Return Code

In this section, I want to mention that you should be careful about using signaling values like -1, null, undefined, etc., for handling errors. This is the wrong approach and makes your code more complicated, so please avoid using this pattern.

Next, pay attention to your code structure you shouldn’t mix normal code logic with error handling. Doing so makes your code hard to read. Instead, use patterns to separate them, such as wrappers.

By far, the best way to clarify and handle errors in your code is using throw and exceptions. This is the most effective method for error detection and handling. Additionally, using type narrowing for your exceptions will make your code even cleaner and more precise.

Do you see? Writing clean code is actually easier than it looks it just requires patience and calmness.

Let’s move on to the next section, my friends. Stay with me!

Don’t Return Null

In this part, I wanna talk about why you should avoid returning null from your functions. Returning null may cause a NullPointerException, and handling such errors can make your code unnecessarily complex and messy. So, be careful about the values you return. Before returning any value, take a moment to ensure it’s valid. This small step will help keep your code clean and elegant.

Define Exception Classes

So in this part of the article, I wanna mention that you should be honest with your code and define the exact exception class you need. Please don’t make your code unnecessarily complex…you can easily manage it by defining the specific error you want to throw. Be clear and explicit with your code.

Don’t forget that every programmer writes their mental health story into their code. Please don’t make me call emergency services to report that I’ve found a psycho programmer who should be arrested ASAP, LOL.

By the way, be honest and humble with your code … that’s the key to writing clean and elegant code.

❌ Bad Example (too generic, not explicit)

function divide(a, b) {
  try {
    return a / b;
  } catch (err) {
    console.error("Something went wrong...");
  }
}

✅ Good Example (explicit & honest)

class DivideByZeroError extends Error {
  constructor(message) {
    super(message);
    this.name = "DivideByZeroError";
  }
}

function divide(a, b) {
  if (typeof a !== "number" || typeof b !== "number") {
    throw new TypeError("Both arguments must be numbers.");
  }

  if (b === 0) {
    throw new DivideByZeroError("Cannot divide by zero.");
  }

  return a / b;
}

try {
  console.log(divide(10, 0));
} catch (err) {
  if (err instanceof DivideByZeroError) {
    console.error("Custom error:", err.message);
  } else {
    console.error("Unexpected error:", err.message);
  }
}

Handle Exceptions At a High Level

Think of your code like a skyscraper. The low-level functions are the basement if something breaks down there, you throw the problem up. But don’t panic! The high-level code is like the control room at the top, that’s where you handle it.

Code is all about layers, abstractions, and a bit of architecture magic. Keep this mantra in mind: throw low, handle high.

That’s the whole trick, mate simple as that. Now, grab your hard hat and let’s step into the next section.

Provide Context With Exceptions

Please give meaningful context for your error exceptions! Nobody wants to end up in a mess just because of a little laziness, dude. Take care of your code.

Define clear, meaningful messages for your errors and decide how you want to throw them. Give me some cute, understandable error messages so I can know what went wrong without spending an hour debugging.

So yeah… PLEASE PROVIDE SOME MEANINGFUL CONTEXT! LMAO :))) Don’t Swallow Exceptions

Don’t Ignore Exceptions

Last but not least, I want to mention: don’t leave your exceptions hanging without any logs! 😅

Please clarify the issue, and if possible, add something like a retry don’t just fail silently at the first error. Make it better, make it understandable. At the very least, log it so your code stays gentle and kind to anyone reading it later.

In short: don’t just throw it or catch it for no reason give it some personality with a log!

Conclusion

Error handling is an essential part of writing clean, maintainable code. By avoiding signaling values like null or -1, defining specific exception classes, providing meaningful context, and handling exceptions at a high level, you make your code not only safer but also easier to read and debug. Remember: separate normal logic from error handling, be honest and explicit with your exceptions, and never swallow errors silently. Writing clean code requires patience and care, but with these principles, you can turn error handling into a strength rather than a headache. Keep calm, stay thoughtful, and let your code speak clearly!

Bye, my friends! I hope you all enjoyed it.

Cheers, Elliot


메타데이터
post_id
1f3ca395183a
slug
clean-code-error-handling-1f3ca395183a
url
https://medium.com/@eliotag/clean-code-error-handling-1f3ca395183a
canonical_url
https://medium.com/@eliotag/clean-code-error-handling-1f3ca395183a
author_url
https://medium.com/@eliotag
status
ok
fetched_at
2026-06-28 14:26:31