Node.js Error Handling

Aim

To demonstrate error handling in a Node.js application.

What is Error Handling?

Error handling is used to detect and handle errors without stopping the entire application.

Example

try {
    let result = 10 / 0;

    if (!isFinite(result)) {
        throw new Error("Invalid calculation");
    }

    console.log(result);
}
catch (error) {
    console.log("Error:", error.message);
}
    

Expected Output

Error: Invalid calculation