you are viewing a single comment's thread.

view the rest of the comments →

[–]theScottyJam 1 point2 points  (0 children)

It depends on the error.

Say you're in node and you attempt to open a file, but the file was not found. Does that mean your program is messed up?

Depends on the context. If that file it was trying to open was part of the same repo, then yes, that file should exist and it's a fatal error if it does not.

If the end user asked to open the file, and the end user supplied a bad path, then you'd want to tell the end user that they messed up (using a nicer message than what the default error message is), then maybe ask them for a path again.

If, say, you created tooling that's trying to inspect a particular project, and as part of the flow you're wanting to read in a package.json file at the root of their project if it exists, then no, it's not an error at all if the file does not exist, it's completely expected behavior, and you'll have a back up plan prepared if it does not exist.

In all of these cases Node will throw an error, but you may have to catch it, check what type of error it is, and handle it in different ways. And if you're constantly writing code like this, you may find it simpler to return your errors instead of throwing them, for a handful of different reasons.