Skip to content
Justice Tech School

Error Types and Custom Errors

Error, TypeError and RangeError are built in. Extend Error for your own and keep the stack.

Example
class NotFound extends Error {
  constructor(what) { super(what + " not found"); this.name = "NotFound"; this.status = 404; }
}
try { throw new NotFound("user"); }
catch (e) { console.log(e.name, e.message, e.status, e instanceof Error); }

Result

NotFound user not found 404 true

Exercise

Custom error.

class E  Error

Quiz Test

What is err.stack?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.