Try and Catch in JavaScript

How can we catch errors in our programs in JavaScript

  1. try-catch block
  2. catch method

try-catch block

Anything inside of try block, if there is any errors then handle them in catch block. Catch block has a parameter about error.

function a() {
  try {
    let num = 1;
    let num2 = 2;
    novalue;
    return num + num2;
  } catch (error) {
    throw error;
  }
}
a();
      throw error
      ^

ReferenceError: novalue is not defined
    at a (/Users/jayjo/Desktop/j.js:23:5)
    at Object.<anonymous> (/Users/jayjo/Desktop/j.js:29:1)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

With Finally block

function a() {
  try {
    let num = 1;
    let num2 = 2;
    novalue;
    return num + num2;
  } catch (error) {
    throw error;
  } finally {
    console.log("It will finally run.");
  }
}
a();
It will finally run.
/Users/jayjo/Desktop/j.js:26
    throw error;
    ^

ReferenceError: novalue is not defined
    at a (/Users/jayjo/Desktop/j.js:23:5)
    at Object.<anonymous> (/Users/jayjo/Desktop/j.js:31:1)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

Even if there is an error in try block, the finally block will be executed.

Nested try-catch block

function a() {
  try {
    try {
      let num = 1;
      let num2 = 2;
      novalue;
      return num + num2;
    } catch (error) {
      throw new Error(error);
    }
  } catch (error) {
    console.log(error);
  }
}
a();
Error: ReferenceError: novalue is not defined
    at a (/Users/jayjo/Desktop/j.js:27:13)
    at Object.<anonymous> (/Users/jayjo/Desktop/j.js:33:1)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

These types of try catch blocks can be used to catch synchronous errors.

However, what if there is asynchronous code in try block?

function a() {
  try {
    setTimeout(function () {
      noValue;
    }, 10000);

    return true;
  } catch (error) {
    console.log(error);
  }
}
console.log(a());
true
/Users/jayjo/Desktop/j.js:22
      noValue;
      ^

ReferenceError: noValue is not defined
    at Timeout._onTimeout (/Users/jayjo/Desktop/j.js:22:7)
    at listOnTimeout (internal/timers.js:549:17)
    at processTimers (internal/timers.js:492:7)

During 10 seconds, catch block can not catch the error. Thus, we have Async Error Handling.

Leave a Reply

Your email address will not be published.

ANOTE.DEV