DEV Community

Simon Mannes
Simon Mannes

Posted on • Originally published at mannes.tech on

Difference between Fault, Error, and Failure explained with JavaScript

You've heard of the terms fault, error, and failure.

And as software developers, we use them every day. But what do they mean?

One popular definition is this (see “Fundamental Concepts of Dependability”, Avižienis et al.):

  • Fault is the cause of an error
  • Error is the incorrect state of the system that may cause a subsequent failure
  • Failure is when the system does not deliver the expected result

To better understand this definition, let's look at a concrete example.

Imagine a JS function that calls an external API to get search results.

const result = JSON.parse(response.payload)
Enter fullscreen mode Exit fullscreen mode

Let's assume the response object is always present.

But when there is no payload property, a SyntaxError when running that code:

  • The fault is that response.payload is not checked to be a JSON-parsable type
  • The error that appears is a SyntaxError, because we try to call JSON.parse(undefined)
  • A failure will now appear if the error is not caught and either the app stops working, or the user gets no results and no error message

This example is a bit trickier than we see at first glance:

  • There will be no failure if the error is caught using try and catch and handled appropriately
  • If the error is caught, you can't really speak of a fault, because the program works as expected
  • But even without fault and failure, the error will still exist for a brief moment, before being caught

Now the big question, where does a bug fit into this?

The bug can be the fault or the failure, depending on context.

  • Bug as fault: “this code has a bug, it will throw an error when the list has no items. Better add an if to handle this case.”
  • Bug as failure: “the software has a bug when I click ‘delete’ with no items on the list.”

So a bug can actually be two things!
Who would've thought 🤓


🙏 Follow me on Twitter @simon_mannes

Top comments (0)