Error Handling in JS - Compressed
Error Handling in JS - Compressed
Assignment Solutions
Assignment Solutions
Answer the following questions briefly
Proper error handling is crucial for maintaining the stability and reliability of a JavaScript application. It
prevents unexpected crashes, provides meaningful feedback to developers during debugging, and
enhances the user experience by gracefully handling issues without abrupt failures.
b. The try...catch statement in JavaScript is used to handle exceptions or errors that may occur within a block
of code. The try block contains the code that might throw an exception, and the catch block specifies what
to do if an exception is thrown.
If an error occurs within the try block, the control is transferred to the corresponding catch block. This allows
developers to handle errors gracefully, log diagnostic information, and take appropriate actions to prevent
the application from crashing.
Using try...catch promotes robust error handling, making it possible to identify and respond to errors without
disrupting the overall flow of the application.
b. How can you create custom error objects in JavaScript, and why would you want to do this?
Ans)
a. The throw statement in JavaScript is used to manually generate an exception or error. When a specific
condition is not met, or an error is detected, the throw statement allows developers to interrupt the normal
flow of the program and trigger an exception.
The throw statement is often used in conjunction with try...catch blocks to handle exceptions gracefully. It
allows developers to create more meaningful error messages and communicate the nature of the error to
aid in debugging.
b. In JavaScript, you can create custom error objects by extending the built-in Error constructor. This allows you
to define your own error types with specific properties and behaviors.
Custom errors provide clearer information about the nature of the error. Developers can distinguish between
different types of errors and handle them appropriately.
Creating a hierarchy of custom errors allows for more organized and structured error handling in complex
applications.
Custom errors can carry additional information, aiding in debugging and providing insights into the cause of
the error.
Write a JavaScript function called calculateAverage that takes an array of numbers as an argument. This
function should calculate and return the average of the numbers in the array. However, you should
implement error handling in the following scenarios:
a. If the input argument is not an array, throw a CustomError with code 400 and the message "Input must be
an array."
b. If the array is empty, throw a CustomError with code 401 and the message "Array must not be empty."
c. If any element in the array is not a number, throw a CustomError with code 402 and the message "Array
must contain only numbers."
d. Handle these custom errors gracefully in your code and provide helpful error messages.
constructor(code, message) {
super(message);
this.code = code;
function calculateAverage(numbers) {
if (!Array.isArray(numbers)) {
if (numbers.length === 0) {
return average;
// Example usage:
try {
console.log("Average:", result);
} catch (error) {
} else {
You are tasked with creating a form validation function in JavaScript for a user registration form. The
form contains fields for username, email, and password. Your goal is to implement error handling to
validate user input and display appropriate error messages.
i. Ensure the username field is not empty and has a minimum length of 3 characters.
ii. Validate the email field to ensure it contains a valid email format (e.g., user@example.com).
c. If any of the validations fail, throw custom error objects specific to each validation.
d. Use a try...catch block within the validateForm function to catch and handle any thrown errors.
constructor(field, message) {
super(message);
this.field = field;
try {
// Validate username
// Validate email
if (!email || !email.match(emailRegex)) {
// Validate password
return true;
} catch (error) {
displayError(error.field, error.message);
} else {
return false;
if (errorMassage) {
console.log(errorMassage)
// Example usage: