LearnFrenzy
Sign in Take a free test
JS

JavaScript Programming · Topic 24 of 25

Error Handling Questions and Concepts

  • 4 exercises
  • 60 questions
  • ~60 min
  • Answers explained

Interview Questions

Error Handling · Study material

Error Handling – Interview Questions

1. How does try/catch/finally work?

try runs the code; if it throws, control jumps to catch with the error; finally runs afterwards in every case, even after return, break or a new throw.

2. What happens if finally contains a return?

It overrides any return value or thrown error from try/catch – the original error is silently lost. Avoid control flow in finally.

3. Name the built-in error types.

Error, TypeError, ReferenceError, SyntaxError, RangeError, URIError, EvalError and AggregateError.

4. Can you throw something that is not an Error?

Yes, any value – but you lose the stack trace and type checks, so always throw Error objects (or subclasses).

5. How do you create a custom error?

class MyError extends Error { constructor(msg, extra) { super(msg); this.name = "MyError"; this.extra = extra; } }

6. What is error.cause?

An ES2022 option, new Error(message, { cause }), that attaches the underlying error when you wrap it.

7. Why can't try/catch catch an error inside setTimeout?

The callback runs later on a new call stack, after the try block has finished. Put the try/catch inside the callback.

8. How do you handle errors in async/await?

Wrap awaits in try/catch, or call .catch on the returned promise. Promise.allSettled collects multiple outcomes.

9. What is an unhandled promise rejection?

A rejected promise with no handler. Node.js terminates by default; browsers log it and fire unhandledrejection.

10. What is optional catch binding?

catch { … } without (e) – allowed since ES2019 when you do not need the error object.

11. Is a SyntaxError catchable?

Only when it happens at run time (JSON.parse, new Function, eval). A syntax error in the script itself stops it from running at all.

12. Why should you not swallow errors?

An empty catch hides failures, so the program continues with bad data and the real cause becomes hard to find.

13. What is AggregateError?

An error holding several errors in its errors array; Promise.any rejects with it, and you can throw it for multi-field validation.

14. How do you check an error's type?

instanceof (works with subclasses), or the name property when errors cross realms or come from serialised data.

15. Exceptions vs returned errors – when to use which?

Throw for unexpected failures and bugs; return a result (null, default, [err, value]) when failure is an ordinary, expected outcome.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About JavaScript Programming

Why it matters

  • JavaScript runs every web page and, with Node.js, servers, tools and APIs – it is the most used language in the world
  • Front-end, full-stack and MERN roles test JavaScript first, and many campus coding rounds allow it
  • "Predict the output" questions on hoisting, closures, this, coercion and the event loop are interview favourites
  • Modern ES6+ – let/const, arrow functions, destructuring, promises and async/await – is what companies expect from freshers

Who it is for

  • College students (B.Tech, BCA, MCA, B.Sc CS) learning web development and preparing for semester exams and viva
  • Freshers preparing for campus placements, online assessments and front-end interviews
  • Developers moving to React, Angular, Vue or Node.js who need strong core JavaScript
  • Anyone revising ES6 and asynchronous JavaScript before an interview

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • Topic-wise MCQs on concepts and ES6 features
  • "Predict the output" puzzles – hoisting, coercion, closures, promises and timers
  • "Find the error" debugging: SyntaxError, ReferenceError, TypeError and silent logic bugs
  • Coding problems with solutions and real output, every one run on Node.js – run any study example in your browser

Asked in

  • Campus placements
  • TCS, Infosys, Wipro, Accenture, Cognizant tests
  • Front-end developer interviews
  • MERN / full-stack interviews
  • Online coding assessments
  • University practicals

How to use this section

  1. Read the study material of a topic and run the examples in the browser.
  2. Practise the four exercises – MCQs, output, debugging, coding problems – each marked Easy, Medium or Hard, and read every explanation.
  3. Revise with the interview questions and cheat sheet, then move to the next topic.

Search LearnFrenzy

Esc

Quick links