What are uncaught exceptions in Node.js?

What are uncaught exceptions (and exceptions in general) in Node.js?

All the resources are about how to handle them, but I haven't found anything explaing what they are and why they happen.

An exception happens when code does something it probably shouldn't. There are a ton of types of exceptions for all kinds of things.

For example:

var array = ["A", "B", "C"];
var s = array[1357].toLowerCase();
// TypeError: Cannot read property 'toLowerCase' of undefined

someOther.code().toRun(); // this will NOT run, execution is aborted at the exception

That's an exception.

Uncaught simply means that no code was looking for that execption so that it could be gracefully handled. Uncaught exceptions halt execution of your code and show up as error in the console. Uncaught exceptions are a very bad thing in production code.

You make an uncaught exception caught with a try/catch block. Which you probably read about in all those "how to handle them" resources you found.

try {
  var array = ["A", "B", "C"];
  var s = array[1357].toLowerCase();
} catch (e) {
  console.log("Don't do that, seriously");
}
someOther.code().toRun(); // this does run, execution continues after caught exception

An exception is basically when something "breaks". For example:

alert(x);

will cause a "ReferenceError: x is not defined" because x has yet to be defined. This is an uncaught exception.

One way to handle exceptions is to wrap them in a simple try/catch:

try { 
  alert(x)
}
catch (e) {
  alert("x wasn't defined");
}

To keep your code running smoothly, you'll want to try and catch and handle all potential exceptions, otherwise your script will stop processing.

Read more on MDN