Loading topic
name, message, and stack, which the previous chapter covered. Catching errors gets a full treatment in the next chapter. Here, try/catch shows up only in a few5 min readname, message, and stack. It also assumes you know how the throw statement stops a function. Here you will learn the working model of try and catch: where control goes 4 min readfinally block holds that code. This chapter covers when finally runs, how it interacts with5 min readError objects, how to use throw, and how to wrap code in try/catch. It also assumes you can run small scripts in Node.js or a browser console. JavaScript has a small set of error constructors built in. The engine uses them to report what k5 min readTypeError and RangeError describe problems with the language itself: a value of the wrong type, or a number out of range. They don't describe problems in your application, such as "this form field is invalid," "this user does not exist," or "the payment provider refuse5 min readtry and catch work, how to throw errors, and how to define a custom error class that extends Error. It also assumes you can run scripts in Node.js, because some examples use the node:fs module.5 min readtry and catch work, how to read an Error's name and message, and how to check a caught error with instanceof and rethrow it. All of that was covered in earlier chapters. Here you will learn a small piece of syntax added in ES2019: a catch clause 6 min readtry/catch blocks, throw errors, define a class that extends Error, and check errors with instanceof. Those topics come in earlier chapters. Here the focus is one narrow problem: how to add meaning to an error without losing the original one.5 min readsetTimeout does, and you have used try and catch on ordinary synchronous code. It explains one of the most common surprises in JavaScript error handling. Code can sit inside a try block on the page and sti6 min readnew Promise, .then(), and Promise.resolve(). It also assumes you have read the earlier chapters on try/catch and on why try/catch misses errors in callbacks that run later. All examples run in Node.js 18 or later, or in a brow5 min readtry, catch, finally, and promise rejections. Every example runs in Node.js 18 or later, or in a browser console.5 min readtry/catch around synchronous code, .catch() on promise chains, and try/catch around await. Some errors still get past all of these. A callback can throw after its surrounding try has finished, someone can forget a .catch(), 5 min readtry, catch, finally, custom error classes, instanceof checks, the cause option, promise rejections, and global handlers. This chapter is about judgment. For any error, it asks where in your code it should be caught, who should log it, and when it6 min read