Error Propagation

Propagate errors through call stack

IntermediateTopic: Error Handling
Back

JavaScript Error Propagation Program

This program helps you to learn the fundamental structure and syntax of JavaScript programming.

Try This Code
// Method 1: Re-throw error
function level1() {
    try {
        level2();
    } catch (error) {
        console.error('Level 1 caught:', error.message);
        throw error; // Re-throw
    }
}

function level2() {
    try {
        level3();
    } catch (error) {
        console.error('Level 2 caught:', error.message);
        throw error; // Re-throw
    }
}

function level3() {
    throw new Error('Error from level 3');
}

try {
    level1();
} catch (error) {
    console.error('Final catch:', error.message);
}

// Method 2: Wrap error
function processData(data) {
    try {
        return JSON.parse(data);
    } catch (error) {
        throw new Error(`Failed to process data: ${error.message}`);
    }
}

try {
    processData('invalid');
} catch (error) {
    console.error('Wrapped error:', error.message);
}

// Method 3: Error chain
class ChainedError extends Error {
    constructor(message, cause) {
        super(message);
        this.cause = cause;
        this.name = 'ChainedError';
    }
}

function operation1() {
    try {
        operation2();
    } catch (error) {
        throw new ChainedError('Operation 1 failed', error);
    }
}

function operation2() {
    throw new Error('Operation 2 failed');
}

try {
    operation1();
} catch (error) {
    console.error('Chained error:', error.message);
    console.error('Caused by:', error.cause.message);
}

// Method 4: Error handling middleware
function errorHandler(error, context) {
    console.error('Error in', context, ':', error.message);
    // Log to service, send notification, etc.
}

function riskyOperation() {
    try {
        throw new Error('Operation failed');
    } catch (error) {
        errorHandler(error, 'riskyOperation');
        throw error;
    }
}
Output
Level 3 caught: Error from level 3
Level 2 caught: Error from level 3
Level 1 caught: Error from level 3
Final catch: Error from level 3
Wrapped error: Failed to process data: Unexpected token i in JSON at position 0
Chained error: Operation 1 failed
Caused by: Operation 2 failed
Error in riskyOperation : Operation failed

Understanding Error Propagation

Error propagation moves errors up call stack.

Re-throwing

Catch and re-throw
Preserve original error
Add context if needed

Wrapping Errors

Wrap in new error
Add context
Preserve original

Error Chains

Link errors
Track cause
Full error history

Error Middleware

Centralized handling
Logging
Notifications

Best Practices

Re-throw when appropriate
Add context
Don't swallow errors
Log properly

Let us now understand every line and the components of the above program.

Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.

Practical Learning Notes for Error Propagation

This JavaScript program is part of the "Error Handling" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents