Error and exception handling is an integral part of programming,

Arpit Bhayani

Arpit Bhayani

Dec 28, 2023 • 1 min read


Error and exception handling is an integral part of programming, here are 6 different ways to handle error ⚡

  1. throw a general exception with a detailed error message
  2. return a special value, like null, suggesting an error
  3. return integer codes - 0 for success and non-zero for errors
  4. pass the error obj as an argument and let the calling function populate it
  5. monads with optional states for different error types
  6. returning the error object as the return value of the function

There is no one right way to handle errors and picking one over another depends on the support provided by the language and your preference. However, being aware of different ways does help you design an elegant non-complicated error-handling mechanism.

⚡ I keep writing and sharing these engineering nuggets, so if you are keen on learning them, follow along.

youtube.com/c/ArpitBhayani

Arpit Bhayani

Principal Engineer II at Razorpay - building Agent Studio, Ex-staff engg at GCP Memorystore & Dataproc, Creator of DiceDB, ex-Amazon Fast Data, ex-Director of Engg. SRE and Data Engineering at Unacademy. I spark engineering curiosity through my no-fluff engineering videos on YouTube and my courses