🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

Error Objects

AI & DATA SCIENCE // error-objects

JavaScript has built-in Error types: Error, TypeError, RangeError, ReferenceError, SyntaxError, and more. Each has name, message, and stack properties.

Syntax

const err = new Error('Something failed');
console.log(err.name);    // 'Error'
console.log(err.message); // 'Something failed'
console.log(err.stack);   // stack trace

Deep Dive Course

JavaScript's built-in Error hierarchy: **Error** (base), **TypeError** (wrong type), **RangeError** (out of range), **ReferenceError** (undefined variable), **SyntaxError** (invalid code), **URIError** (malformed URI). The **stack** property contains the full call stack trace — invaluable for debugging.

1Understanding Error Objects

JavaScript's built-in Error hierarchy: Error (base), TypeError (wrong type), RangeError (out of range), ReferenceError (undefined variable), SyntaxError (invalid code), URIError (malformed URI). The stack property contains the full call stack trace — invaluable for debugging.

💡

In catch blocks, use instanceof to handle different error types differently: if (e instanceof TypeError) { ... }

editor.html
// Built-in error types
try { null.property; }     // TypeError
catch (e) { console.log(e instanceof TypeError, e.name); }

try { undefinedVar; }      // ReferenceError
catch (e) { console.log(e instanceof ReferenceError, e.name); }

try { new Array(-1); }    // RangeError
catch (e) { console.log(e instanceof RangeError, e.name); }
localhost:3000

2Practical Example

Here is a real-world application of Error Objects showing how it is used in production JavaScript code.

editor.html
// Use instanceof for specific handling
function handleError(e) {
  if (e instanceof TypeError) {
    console.log('Type problem:', e.message);
  } else if (e instanceof RangeError) {
    console.log('Out of range:', e.message);
  } else {
    console.log('Unexpected error:', e.message);
    throw e; // re-throw unknown errors
  }
}
localhost:3000

3Best Practices

Follow these guidelines when working with Error Objects:

1. Use specific error types when possible (TypeError, RangeError)

2. Inspect e.stack in development for debugging

3. Use instanceof for type-specific error handling

⚠️

Tip: In catch blocks, use instanceof to handle different error types differently: if (e instanceof TypeError) { ... }

editor.html
// Built-in error types
try { null.property; }     // TypeError
catch (e) { console.log(e instanceof TypeError, e.name); }

try { undefinedVar; }      // ReferenceError
catch (e) { console.log(e instanceof ReferenceError, e.name); }

try { new Array(-1); }    // RangeError
catch (e) { console.log(e instanceof RangeError, e.name); }
localhost:3000

Examples

Example 01Basic Usage
// Built-in error types
try { null.property; }     // TypeError
catch (e) { console.log(e instanceof TypeError, e.name); }

try { undefinedVar; }      // ReferenceError
catch (e) { console.log(e instanceof ReferenceError, e.name); }

try { new Array(-1); }    // RangeError
catch (e) { console.log(e instanceof RangeError, e.name); }
Example 02Advanced Example
// Use instanceof for specific handling
function handleError(e) {
  if (e instanceof TypeError) {
    console.log('Type problem:', e.message);
  } else if (e instanceof RangeError) {
    console.log('Out of range:', e.message);
  } else {
    console.log('Unexpected error:', e.message);
    throw e; // re-throw unknown errors
  }
}

Best Practices

  • Use specific error types when possible (TypeError, RangeError)
  • Inspect e.stack in development for debugging
  • Use instanceof for type-specific error handling

Interview Question

What built-in error should you throw for a wrong type argument?

Hint: Type mismatch.

Throw TypeError for wrong types: 'throw new TypeError("Expected a number, got string")'. Use RangeError for out-of-range values (negative array length, invalid dates). Use ReferenceError only when intentionally simulating a reference failure — it's normally thrown by the engine itself.

Exercises

MediumPractice using Error Objects in a real scenario.
View Solution
// Built-in error types
try { null.property; }     // TypeError
catch (e) { console.log(e instanceof TypeError, e.name); }

try { undefinedVar; }      // ReferenceError
catch (e) { console.log(e instanceof ReferenceError, e.name); }

try { new Array(-1); }    // RangeError
catch (e) { console.log(e instanceof RangeError, e.name); }

Frequently Asked Questions

What built-in error should you throw for a wrong type argument?

Throw TypeError for wrong types: 'throw new TypeError("Expected a number, got string")'. Use RangeError for out-of-range values (negative array length, invalid dates). Use ReferenceError only when intentionally simulating a reference failure — it's normally thrown by the engine itself.

Related Functions

throwtrycatchCustom-Exceptions