DEV Community

Danphe
Danphe

Posted on

1 2

Guess the error message

Can you guess the correct error message this program will throw when executed, also do mention why?

class HookExecutor {
  constructor() {
    this.hooks = [];
  }

  async exec() {
    try {
      this.hooks.forEach((fn) => fn());
    } catch (Err) {
      console.log(
        "Error 3: caught inside exec function where all hooks are executed."
      );
    }
  }

  process() {
    this.exec().catch((error) => {
      console.log("Error 2: caught inside new HooksExecutor.process", error);
    });
  }
}

const hookExecutor = new HookExecutor();
try {
  hookExecutor.hooks.push(async () => {
    console.log("I am a hooks");
    throw new Error("Serious Error!");
  });
} catch (err) {
  console.log("Error 1: Caught in global space", err);
}

hookExecutor.process();
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay