Error and exception handling is an integral part of programming, here are 6 different ways to handle error ⚡
- throw a general exception with a detailed error message
- return a special value, like null, suggesting an error
- return integer codes - 0 for success and non-zero for errors
- pass the error obj as an argument and let the calling function populate it
- monads with optional states for different error types
- 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