Do not define dynamic errors, use wrapped static errors instead




Static Error:

This error occurs due to a known issue in the code or a mistake in the implementation that is not dependent on user input or dynamic conditions.

Example 1:

This is a simple example of a static error:


function addNumbers(a, b) {
return a + b;
}

// Calling the function with incorrect parameters
var result = addNumbers(5, "10"); // Static error: Trying to add a Number with a String

Wrapped Static Error:

A wrapped static error is a type of static error that is surrounded within conditional statements or try-catch blocks to provide a specific error message based on predefined conditions.

Example 2:

This example shows a wrapped static error:


function divideNumbers(a, b) {
if (b == 0) {
throw new Error("Cannot divide by zero!"); // Wrapped static error: Division by zero
} else {
return a / b;
}
}

// Calling the function with zero as the divisor
try {
var result = divideNumbers(10, 0); // Wrapped static error will be thrown
} catch (error) {
console.log(error.message); // Output: Cannot divide by zero!
}


Read more

Leave a comment