0
0
Javascriptprogramming~10 mins

Throwing errors in Javascript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Throwing errors
Start
Check condition
Yes
Throw error
Catch error?
NoProgram stops
Yes
Handle error
Continue or Exit
The program checks a condition, throws an error if needed, then either catches and handles it or stops.
Execution Sample
Javascript
function checkAge(age) {
  if (age < 18) {
    throw new Error('Too young');
  }
  return 'Allowed';
}

checkAge(15);
This code throws an error if age is less than 18, otherwise returns 'Allowed'.
Execution Table
StepActionConditionResultOutput/Error
1Call checkAge(15)age = 15Check if 15 < 18No output yet
2Condition true15 < 18Throw new Error('Too young')Error thrown: Too young
3No catch blockError not caughtProgram stopsUncaught Error: Too young
💡 Error thrown at step 2 and not caught, so program stops with uncaught error.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
ageundefined151515
Key Moments - 2 Insights
Why does the program stop after throwing the error?
Because there is no catch block to handle the error, as shown in execution_table step 3, the error is uncaught and stops the program.
What happens if the condition is false?
If the condition is false, the throw statement is skipped and the function returns normally, but in this example, the condition is true (step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 2?
A'Allowed'
BError thrown: Too young
CNo output
DProgram continues normally
💡 Hint
See execution_table row 2, the error is thrown when age < 18.
At which step does the program stop due to an uncaught error?
AStep 1
BStep 2
CStep 3
DIt never stops
💡 Hint
Check execution_table row 3, program stops because error is not caught.
If we call checkAge(20), what would change in the execution table?
ACondition false, function returns 'Allowed'
BProgram stops at step 3
CError thrown at step 2
DError thrown at step 1
💡 Hint
Refer to variable_tracker and condition check in execution_table step 2.
Concept Snapshot
Throw errors with throw new Error('message')
Use if conditions to decide when to throw
If error not caught, program stops
Use try-catch to handle errors
Throwing interrupts normal flow
Errors carry messages for debugging
Full Transcript
This example shows how JavaScript throws errors using the throw statement. The function checkAge checks if the age is less than 18. If yes, it throws an error with the message 'Too young'. The execution table traces the steps: calling the function with 15, checking the condition, throwing the error, and stopping the program because there is no catch block. Variables like age are tracked through the steps. Key moments explain why the program stops and what happens if the condition is false. The quiz asks about the output at throwing, when the program stops, and what changes if age is 20. The snapshot summarizes how throwing errors works and how it affects program flow.