try catch Statements in JavaScript

In JavaScript, the try…catch statement is used to handle errors gracefully. Instead of stopping the execution of your code when an error occurs, it allows you to “catch” the error and execute alternative code to handle it. This is especially useful for debugging and building resilient applications.

1. Syntax of try…catch

try {

  // Code that may throw an error

} catch (error) {

  // Code to handle the error

}

  • try block: Contains code that might throw an error.
  • catch block: Executes if an error occurs in the try block. It receives an error object with details about the error.

Example:

try {

  const result = 10 / 0; // No error here

  console.log(result);

} catch (error) {

  console.log(“An error occurred:”, error.message);

}

2. Using finally with try…catch

The finally block contains code that will always execute, regardless of whether an error was thrown or not. It’s typically used for cleanup actions.

Example:

try {

  console.log(“Trying…”);

} catch (error) {

  console.log(“Error caught:”, error.message);

} finally {

  console.log(“Execution completed.”);

}

Output:

Trying…

Execution completed.

3. Throwing Custom Errors

The throw statement allows you to manually throw an error.

Example:

try {

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

} catch (error) {

  console.log(“Caught:”, error.message);

}

4. Nested try…catch

You can nest try…catch statements for handling specific errors separately.

Example:

try {

  try {

    JSON.parse(“{ invalid: json }”);

  } catch (innerError) {

    console.log(“Inner error caught:”, innerError.message);

  }

} catch (outerError) {

  console.log(“Outer error caught:”, outerError.message);

}

5. Key Properties of the Error Object

  • message: The error message.
  • name: The type of error (e.g., TypeError, SyntaxError).
  • stack: The stack trace (useful for debugging).

Example:

try {

  null.method();

} catch (error) {

  console.log(“Error name:”, error.name);

  console.log(“Error message:”, error.message);

}

Multiple-Choice Questions

What is the purpose of the try block?

  1. To catch and handle errors.
  2. To execute code that may throw an error.
  3. To always execute cleanup code.
  4. To stop code execution when an error occurs.

Answer: 2. To execute code that may throw an error.
Explanation: The try block contains code that might throw an error.

What happens if no error occurs in the try block?

  1. The catch block executes.
  2. The catch block is skipped.
  3. The finally block is skipped.
  4. An error is thrown automatically.

Answer: 2. The catch block is skipped.
Explanation: The catch block only executes if an error occurs in the try block.

What is the purpose of the catch block?

  1. To execute code after the try block.
  2. To handle errors thrown in the try block.
  3. To throw errors manually.
  4. To skip error handling.

Answer: 2. To handle errors thrown in the try block.
Explanation: The catch block executes when an error occurs in the try block.

What does this code do?

try {

  console.log(variable);

} catch (error) {

  console.log(“Error caught:”, error.message);

}

  1. Logs the value of variable.
  2. Logs “Error caught: variable is not defined”.
  3. Throws an error and stops execution.
  4. Skips the catch block.

Answer: 2. Logs “Error caught: variable is not defined”.
Explanation: The try block throws a ReferenceError because variable is not defined.

What is the purpose of the finally block?

  1. To execute only if an error occurs.
  2. To execute code regardless of whether an error occurred or not.
  3. To stop the execution of the program.
  4. To handle specific types of errors.

Answer: 2. To execute code regardless of whether an error occurred or not.
Explanation: The finally block always executes after the try and catch blocks.

What does this code do?

try {

  console.log(“Inside try”);

} finally {

  console.log(“Inside finally”);

}

  1. Logs “Inside try” only.
  2. Logs “Inside finally” only.
  3. Logs “Inside try” followed by “Inside finally”.
  4. Throws an error.

Answer: 3. Logs “Inside try” followed by “Inside finally”.
Explanation: The finally block always executes, even if no error occurs.

What happens if there is no catch block after a try?

  1. The program throws an error.
  2. The program skips error handling.
  3. The program executes the finally block if it exists.
  4. The program terminates.

Answer: 3. The program executes the finally block if it exists.
Explanation: The finally block executes regardless of whether a catch block is present.

Which of the following statements throws a custom error?

  1. throw new Error(“Custom error”);
  2. console.error(“Custom error”);
  3. console.log(“Custom error”);
  4. return “Custom error”;

Answer: 1. throw new Error(“Custom error”);
Explanation: The throw statement explicitly raises a custom error.

What does this code output?

try {

  throw new Error(“Something went wrong”);

} catch (error) {

  console.log(error.message);

}

  1. “Error”
  2. “Something went wrong”
  3. undefined
  4. “error.message”

Answer: 2. “Something went wrong”
Explanation: The message property of the Error object contains the error message.

What is the role of the Error object in JavaScript?

  1. To log errors to the console.
  2. To represent an error, including its name and message.
  3. To terminate the program.
  4. To prevent errors from occurring.

Answer: 2. To represent an error, including its name and message.
Explanation: The Error object encapsulates details about an error, such as its name and message.