JS Error Handling

HTML
CSS
C#
SQL

Error Handling

try, catch, and finally Statements:

try: Defines a block of code to run (to try).
catch: Defines a block of code to handle any error.
finally: Defines a block of code to run regardless of the result.

Example:

1. throw Statement:

– Defines a custom error.
– Used to throw an exception.

Example:

throw new Error(“This is a custom error message.”);

try and catch Pair:

– The try statement allows you to define a block of code to be tested for errors while it is being executed.
– The catch statement allows you to define a block of code to be executed if an error occurs in the try block.

Syntax:

try {
  // Block of code to try
}
catch(err) {
  // Block of code to handle errors
}

Example:

try {
// Code that may throw an error
let result = someUndefinedVariable * 2;
console.log(result); // This line will not be executed
} catch (error) {
// Handle the error
console.error(“An error occurred:”, error.message);
}

Course Video

Examples for Practice

You have to solve all the questions given below in the editor without copy-pasting.

1. Print the Error using Error Handling and also Print ” I am Finally “Whether the code shows Error Or Not.

The task is to use error handling in JavaScript to catch and print an error, and then print a message regardless of whether an error occurred or not.
1. Try Block:
Use a try block to wrap the code that might throw an error.
2. Catch Block:
Use a catch block to catch and handle the error. Inside the catch block, log the error.
3. Finally Block:
Use a finally block to include code that will be executed regardless of whether an error occurred or not. Log the message “I am Finally” inside the finally block.

Here’s an example program:

try {
// Code that might throw an error
// For example, trying to access an undefined variable
console.log(undefinedVariable); // This will throw a ReferenceError
} catch (error) {
// Catch block to handle the error
console.error(“Error:”, error.message);
} finally {
// Finally block to execute code regardless of an error
console.log(“I am Finally”);
}

Output

2. Write a function that takes two parameters (a and b) and calculates the division of a by b. Implement error handling to check if b is zero and throw a custom error in such a case. Catch the error and log an appropriate message. Test the function with different values of a and b, including scenarios where b is zero.

The task is to write a JavaScript function that performs division of two parameters (a and b). The function should include error handling to check if b is zero, and if so, it should throw a custom error. You need to catch this error and log an appropriate message. Finally, test the function with different values of a and b, including scenarios where b is zero.
1. Write a Function:
Define a function that takes two parameters a and b.
2. Error Handling:
– Use error handling to check if b is zero.
– If b is zero, throw a custom error using the throw statement.
3. Catch Block:
Use a catch block to catch the custom error and log an appropriate message.
4. Test the Function:
Test the function with different values of a and b, including scenarios where b is zero.

Here’s an example program:

function divide(a, b) {
try {
// Check if b is zero
if (b === 0) {
// Throw a custom error if b is zero
throw new Error(“Division by zero is not allowed”);
}

// Perform division
return a / b;
} catch (error) {
// Catch block to handle the custom error
console.error(“Error:”, error.message);
}
}

// Test the function with different values of a and b
console.log(divide(10, 2)); // Output: 5
console.log(divide(5, 0)); // Output: Error: Division by zero is not allowed

Output