"use strict";
/*
* Error Object Example
* --------------------
* Demonstrates:
* - Creating Error objects
* - Throwing errors
* - Catching errors
* - Custom Error classes
* - Error handling
* - try/catch/finally
*/
/*
JavaScript provides several built-in error types
------------------------------------------------
Error
TypeError
ReferenceError
RangeError
SyntaxError
URIError
EvalError
AggregateError
*/
/*
Core concepts include
---------------------
throw
try
catch
finally
Error
CustomError
instanceof
*/
/**
* Custom validation error.
*/
class ValidationError extends Error {
constructor(message) {
super(message);
this.name = "ValidationError";
}
}
/**
* Divides two numbers.
*
* @param {number} dividend
* @param {number} divisor
* @returns {number}
* @throws {Error}
*/
function divide(dividend, divisor) {
if (divisor === 0) {
throw new Error(
"Division by zero is not allowed."
);
}
return dividend / divisor;
}
/**
* Validates a user's age.
*
* @param {number} age
* @returns {boolean}
* @throws {ValidationError}
*/
function validateAge(age) {
if (age < 0) {
throw new ValidationError(
"Age cannot be negative."
);
}
if (age > 130) {
throw new ValidationError(
"Age exceeds valid range."
);
}
return true;
}
/**
* Centralized error handler.
*
* @param {Error} error
*/
function handleError(error) {
console.error("Error Type :", error.name);
console.error("Message :", error.message);
if (error.stack) {
console.error("Stack Trace:");
console.error(error.stack);
}
}
// --------------------------------------------------
// Creating an Error Object
// --------------------------------------------------
console.log("=== Create Error ===");
const error = new Error(
"Something went wrong."
);
console.log(error.name);
console.log(error.message);
// --------------------------------------------------
// Example 1: Successful Division
// --------------------------------------------------
console.log("\n=== Successful Division ===");
try {
const result = divide(10, 2);
console.log("Result:", result);
} catch (error) {
handleError(error);
}
// --------------------------------------------------
// Example 2: Division by Zero
// --------------------------------------------------
console.log("\n=== Division by Zero ===");
try {
const result = divide(10, 0);
console.log(result);
} catch (error) {
handleError(error);
}
// --------------------------------------------------
// Example 3: Valid Age
// --------------------------------------------------
console.log("\n=== Valid Age ===");
try {
validateAge(25);
console.log(
"Age validation successful."
);
} catch (error) {
handleError(error);
}
// --------------------------------------------------
// Example 4: Invalid Age
// --------------------------------------------------
console.log("\n=== Invalid Age ===");
try {
validateAge(-5);
} catch (error) {
handleError(error);
}
// --------------------------------------------------
// Example 5: Error Type Checking
// --------------------------------------------------
console.log("\n=== instanceof Example ===");
try {
validateAge(500);
} catch (error) {
if (error instanceof ValidationError) {
console.log(
"Validation Error:",
error.message
);
} else if (error instanceof Error) {
console.log(
"General Error:",
error.message
);
}
}
// --------------------------------------------------
// Example 6: Finally Block
// --------------------------------------------------
console.log("\n=== Finally Example ===");
try {
console.log("Processing...");
throw new Error("Process failed.");
} catch (error) {
handleError(error);
} finally {
console.log(
"Cleanup completed."
);
}
// --------------------------------------------------
// Example 7: Error Properties
// --------------------------------------------------
console.log("\n=== Error Properties ===");
const customError = new Error(
"Custom error message."
);
console.log("Name:", customError.name);
console.log("Message:", customError.message);
console.log(
"Has Stack:",
customError.stack !== undefined
);
/*
run:
=== Create Error ===
Error
Something went wrong.
=== Successful Division ===
Result: 5
=== Division by Zero ===
Error Type : Error
Message : Division by zero is not allowed.
Stack Trace:
ERROR!
Error: Division by zero is not allowed.
=== Valid Age ===
Age validation successful.
=== Invalid Age ===
Error Type : ValidationError
Message : Age cannot be negative.
ERROR!
Stack Trace:
ValidationError: Age cannot be negative.
=== instanceof Example ===
ERROR!
Validation Error: Age exceeds valid range.
=== Finally Example ===
Processing...
Error Type : Error
Message : Process failed.
ERROR!
Stack Trace:
Error: Process failed.
Cleanup completed.
=== Error Properties ===
Name: Error
Message: Custom error message.
Has Stack: true
*/