In the previous section, we explored how to use the throw statement to create custom errors. However, JavaScript provides several built-in error types that help us handle common issues more effectively. Understanding these error objects and their properties is crucial for robust error handling in your applications.
Error objects in JavaScript are instances of the Error class or its subclasses. They provide information about runtime errors, such as type mismatches or undefined variables. Knowing how to work with these built-in error types can help you write more reliable code and make debugging easier.
In this tutorial, we'll cover two common built-in error types: TypeError and ReferenceError. We'll also explore the properties of error objects, specifically name and message, which are essential for understanding and handling errors effectively.
A TypeError is thrown when an operation is applied to a value of the wrong type. This can happen in various scenarios, such as calling a method on a non-object or using an operator with incompatible types.
1// script.js2let num = 5;3num.toUpperCase();
While this code doesn't throw a TypeError, it demonstrates the importance of type checking. If we were to perform an operation that strictly requires numbers, such as division, a TypeError would occur.
A ReferenceError is thrown when trying to access a variable or function that hasn't been declared or is out of scope.
1// script.js2console.log(undeclaredVariable);
Here, innerFunction is declared inside outerFunction, so it's not accessible outside of outerFunction.
Every error object in JavaScript has several properties that provide information about the error. The two most commonly used properties are name and message.
The name property specifies the type of error. For built-in errors, this is usually a string like "TypeError" or "ReferenceError". You can also set this property for custom errors.
1// script.js2try {3let num = 5;4num.toUpperCase();5} catch (error) {6console.log(error.name); // Output: TypeError7}
Let's create a simple program that demonstrates the use of TypeError and ReferenceError, as well as how to handle them using try-catch blocks.
1// script.js2function divide(a, b) {3if (typeof a !== 'number' || typeof b !== 'number') {4throw new TypeError('Both arguments must be numbers');5}6if (b === 0) {7throw new Error('Division by zero is not allowed');8}9return a / b;10}1112try {13console.log(divide(10, 2)); // Output: 514console.log(divide('ten', 2)); // Throws TypeError15} catch (error) {16console.log(`Error: ${error.name} - ${error.message}`);17}1819try {20console.log(myUndefinedVariable); // Throws ReferenceError21} catch (error) {22console.log(`Error: ${error.name} - ${error.message}`);23}
5 Error: TypeError - Both arguments must be numbers Error: ReferenceError - myUndefinedVariable is not defined
In this example, we define a divide function that checks the types of its arguments and throws appropriate errors if necessary. We then use try-catch blocks to handle these errors gracefully.
TypeError and ReferenceError, which help identify common runtime issues.name and message that provide information about the error type and description, respectively.Now that you understand how to work with built-in error types and their properties, let's explore how to use "use strict" in JavaScript. This feature helps catch common coding mistakes and enforces stricter parsing rules, making your code more robust and secure.
Stay tuned for the next tutorial on "JavaScript 'use strict'".