DEV Community


Discussion on: Five Levels of Error Handling in Both Python and JavaScript

jesterxl profile image
Jesse Warden Author • Edited


Try/catch/throw has 3 different flows in your code. Either your code works, fails, or it breaks. Using return values, your code never breaks; it's simply a return value that states the function didn't work. You don't have to wrap your code with try catch in case "something in there" explodes. This is more complicated in things like JavaScript and Python ThreadPool where async things break and try/catch suddenly don't cover everything. Also, did you remember global error handling so your Docker container can gracefully clean up and fail and K8 can restart? Ugh.

Go's style, or RxJS's pipelines make it really simple: "All this works, and if it doesn't, we'll give you an error and tell you why". No need for try/catch, or the bravery NOT to use try/catch (I'm ignoring Go's panics for now). First, I say "really simple", but not having try/catch is a huge change for people. Secondly, you're asking many are used to OOP based languages to no longer use class; they'll question how they abstract and organize things? Third, you're encouraging returning values. Many OOP languages have side effects and Noop's galore; you call all kinds of functions or class methods that return no value and operate on internal state or send events elsewhere. You never use the return keyword. However, if you're willing to learn this new style, your life is simplified because you no longer have to remember 2 types of code execution: normal and exceptions. Now, it's just "function return values".

Also, my article is Functional Programming biased, specifically typed. If you create pure functions, then Go's style works great because your code is predictable. Unlike an impure function in JavaScript or Python, which works, doesn't work, or throws an exception. Now you have 2 instead of 3. Testing is easier, too. Also, your function can no longer break other functions you use it in. Also, it can no longer break your program; it can't throw or raise exceptions, so it's safe. That's huge. You just negated the need for a throwable keyword in Java. Writing functions that either work or not, can't break your program, are easier to test, and you never need to use try/catch again is wonderful. Now, getting this full benefit in dynamic languages like JavaScript and Python is hard since they don't have types, but it's still a step in a more predictable code direction.