JavaScript Program: Catching and handling RangeError with Try-Catch
JavaScript Error Handling: Exercise-7 with Solution
Handle RangeError for Invalid Index
Write a JavaScript program that uses a try-catch block to catch and handle a 'RangeError' when accessing an array with an invalid index.
Sample Solution:
JavaScript Code:
// Define a function named access_Array_Element that takes two parameters: array and index
function access_Array_Element(array, index) {
// Attempt to access the element at the given index in the array
try {
// Access the element at the specified index in the array
const element = array[index];
// Check if the element is undefined (which happens when index is out of bounds)
if (element === undefined) {
throw new RangeError('Index out of bounds');
}
// Log the accessed element to the console
console.log('Accessed element:', element);
} catch (error) {
// If an error occurs during execution
// Check if the error is a RangeError
if (error instanceof RangeError) {
// If the error is a RangeError, log a message indicating that the index is invalid
console.log('Error: Invalid index. Please provide a valid index within the array bounds.');
} else {
// If the error is not a RangeError, log the error message
console.log('Error:', error.message);
}
}
}
// Example
// Create an array named nums with some elements
const nums = [1, 2, 3, 4, 5];
// Call the access_Array_Element function with nums and index 1, which is a valid index
access_Array_Element(nums, 1); // Valid index
// Call the access_Array_Element function with nums and index 5, which is an invalid index
access_Array_Element(nums, 5); // Invalid index
Output:
Accessed element: Error: Invalid index. Please provide a valid index within the array bounds.
Explanation:
-
Function Definition:
- The function access_Array_Element is defined, which takes two parameters: array (the array to access) and index (the index of the element to access).
- Inside the function, there's a try block where the attempt to access the array element at the specified index is made.
- If an error occurs during this attempt, it's caught by the catch block.
- Within the try block, the element at the specified index in the array is accessed using array[index].
- If the accessed element is undefined (which occurs when the index is out of bounds), a RangeError is thrown.
- If a RangeError occurs (indicating that the index is out of bounds), the error is caught by the catch block.
- If the error is a RangeError, a message indicating an invalid index within the array bounds is logged.
- If the error is not a RangeError, the error message itself is logged.
- An example is provided where the function access_Array_Element is called twice:
- First with a valid index (1) to demonstrate successful access.
- Second with an invalid index (5) to demonstrate error handling for an out-of-bounds index.
Live Demo:
See the Pen javascript-error-handling-exercise-7 by w3resource (@w3resource) on CodePen.
Improve this sample solution and post your code through Disqus.
Error Handling Exercises Previous: Custom Error on an empty string.
Error Handling Exercises Next: Handling different types of errors with multiple catch blocks.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics