So, let’s start with a little backstory about me. I am a software developer with around ten years of experience, initially working with PHP and then gradually transitioning to JavaScript.
I started using TypeScript somewhere around five years ago, and since then, I have never gone back to JavaScript. The moment I started using it, I thought it was the best programming language ever created. Everyone loves it; everyone uses it… it’s just the best one, right? Right? RIGHT?
Yeah, and then I started playing around with other languages, more modern ones. First was Go, and then I slowly added Rust to my list (thanks, Prime).
It’s hard to miss things when you don’t know different things exist.
What am I talking about? What is the common thing that Go and Rust share? Errors. The thing that stood out the most for me. And, more specifically, how these languages handle them.
JavaScript relies on throwing exceptions to handle errors, whereas Go and Rust treat them as values. You might think this is not such a big deal… but, boy, it may sound trivial; however, it’s a game-changer.
Let’s walk through them. We will not dive deep into each language; we want to know the general approach.
Let’s start with JavaScript/TypeScript and a little game.
Give yourself five seconds to review the code below and answer why we need to wrap it in try/catch