0
0
Node.jsframework~10 mins

Custom error classes in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom error classes
Define class extending Error
Create instance of custom error
Throw custom error
Catch error in try-catch
Check error type with instanceof
Handle or log error
End
This flow shows how to define a custom error class, throw it, catch it, and handle it by checking its type.
Execution Sample
Node.js
class MyError extends Error {
  constructor(message) {
    super(message);
    this.name = 'MyError';
  }
}

try {
  throw new MyError('Oops!');
} catch (e) {
  console.log(e.name + ': ' + e.message);
}
Defines a custom error class, throws it, catches it, and logs its name and message.
Execution Table
StepActionEvaluationResult
1Define class MyError extending ErrorClass MyError createdMyError class ready
2Create new MyError with message 'Oops!'new MyError('Oops!')Error object with name 'MyError' and message 'Oops!'
3Throw the MyError instancethrow new MyError('Oops!')Exception thrown, jumps to catch
4Catch error in catch blockcatch(e)e is MyError instance
5Log e.name + ': ' + e.messageconsole.log outputMyError: Oops!
💡 Execution stops after catch block finishes handling the thrown error.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
MyErrorundefinedClass definedClass definedClass definedClass defined
eundefinedundefinedundefinedMyError instanceMyError instance
Key Moments - 3 Insights
Why do we set this.name = 'MyError' inside the constructor?
Because the default Error name is 'Error', setting this.name helps identify the error type clearly in logs, as shown in step 5 of the execution_table.
What happens if we throw the error outside a try-catch block?
The program will crash or stop running because the error is unhandled. The try-catch in step 3 and 4 shows how to catch and handle it safely.
How do we check if the caught error is our custom error?
We use 'instanceof MyError' to check the error type, which is possible because the error object is created from the MyError class as shown in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'e.name' at step 5?
A'MyError'
B'Error'
C'Oops!'
Dundefined
💡 Hint
Check the 'Result' column at step 5 where the error name is logged.
At which step is the custom error instance created?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column where 'new MyError' is called.
If we remove 'this.name = "MyError"' from the constructor, what changes in the output at step 5?
AThe output will not change
BThe output will show 'Error: Oops!' instead of 'MyError: Oops!'
CThe program will crash
DThe error message will be empty
💡 Hint
Refer to the key_moments about setting the error name and the output logged at step 5.
Concept Snapshot
Custom error classes extend Error to create specific error types.
Set this.name in constructor to identify error type.
Throw instances with throw new MyError(message).
Catch with try-catch and check type with instanceof.
Helps organize error handling clearly.
Full Transcript
This lesson shows how to create custom error classes in Node.js by extending the built-in Error class. We define a class MyError that sets its name property to identify the error type. Then we create an instance of MyError with a message and throw it inside a try block. The catch block catches the error object, which is an instance of MyError. We log the error's name and message to show how custom errors can be identified and handled. This helps organize error handling in programs by creating specific error types instead of using generic errors.