Throw & Re-Throw Runtime Errors in JavaScript
JavaScript·2 min read·Jan 14, 2026
In JavaScript, runtime errors can also be triggered programatically on purpose, when the code detects an invalid situation that would prevent it from completing its tasks properly or safely.
Throwing errors allows the code to stop its normal execution flow immediately and signal that something went wrong, like for example, an invalid input or an illogical state. Once an error is thrown, JavaScript will jump out of the current code path and look for a place where that error is handled.
In this lesson, you'll learn how to throw various runtime errors and how to bubble them up through multiple function calls.
Throw runtime errors
In JavaScript, throwing (or raising) errors allows developers to control the flow of an application in the presence of exceptional conditions or unexpected situations.
Errors can be thrown for a multitude of reasons, such as indicating exceptional conditions, preventing further execution of faulty code, enforcing contracts and invariants within your code, and so on.
To raise an error, you can use the throw keyword followed by an instance of the Error class or one of its subclasses:
throw new Error('error message');Beyond the Error class itself, JavaScript has several built-in error types that all inherit from it, including but not limited to:
SyntaxErrorto indicate a syntax error.TypeErrorto indicate an operation was performed on a value of an inappropriate type.RangeErrorto indicate a numeric value is out of range.ReferenceErrorto indicate an invalid reference.EvalErrorto indicate an error regarding the eval function.
💡 In JavaScript, the
throwkeyword can be used to throw any type of data, including strings, numbers, objects, and so on (e.g.,throw 3).
Unlock the program 🚀
Pay once, own it forever.
€79
30-day money-back guarantee
- 13 modules
- 113 lessons with full-code examples
- 29 projects with commented solutions
- All future lesson and project updates
- Lifetime access
By submitting this form, you agree to the Terms & Conditions and Privacy Policy.