Error Throwing
JavascriptError and Stop
Throwing
If your code has detected a problem and needs to stop running, it can throw an error.
throw new Error("Something went wrong!");
This will stop your code and print to console.
You can also use different types of errors:
throw new TypeError("Expected a string");
throw new RangeError("Out of bounds");
throw new SyntaxError("Bad format");
Types of errors
There are 7 types of errors:
| Error Type | Description |
|---|---|
| Error | Standard Generic Error |
| SyntaxError | Error related to syntax |
| ReferenceError | An illegal reference has occured |
| RangeError | A number "out of range" has occured |
| TypeError | Error with the value type |
| EvalError | Error related to the eval() function |
| URIError | Error related to the encodeURI() function |
Error and Continue
If you want to alert an issue but are happy for the code to keep running, you can simply print to console (in red):
console.error("This is an error log but won't stop the script.");
or to simply warn (in yellow):
console.warn("This is just a warning.");