0
0
Javascriptprogramming~10 mins

Catching runtime errors in Javascript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Catching runtime errors
Start
Try block runs
Error occurs?
NoTry block ends normally
Yes
Catch block runs
Finally block runs (optional)
Program continues
The program tries to run code inside try. If an error happens, it jumps to catch. Finally runs always, then program continues.
Execution Sample
Javascript
try {
  let result = 10 / 0;
  console.log('Result:', result);
} catch (error) {
  console.log('Error caught:', error.message);
}
This code tries to divide 10 by 0 and logs the result or catches an error if it happens.
Execution Table
StepActionEvaluationResult
1Enter try blockStart executing code inside tryNo error yet
2Calculate 10 / 010 divided by 0Infinity (no error in JS)
3console.log('Result:', result)Prints 'Result: Infinity'Output: Result: Infinity
4Try block endsNo error thrownCatch block skipped
5Program continuesNo catch block runProgram ends normally
💡 No runtime error occurs because dividing by zero in JavaScript returns Infinity, so catch block is not executed.
Variable Tracker
VariableStartAfter Step 2Final
resultundefinedInfinityInfinity
Key Moments - 2 Insights
Why doesn't the catch block run when dividing by zero?
In JavaScript, dividing by zero does not cause a runtime error; it returns Infinity. So the try block finishes normally (see execution_table step 2 and 4).
What happens if an actual error occurs inside try?
If an error happens, execution jumps immediately to the catch block (not shown here but explained in concept_flow).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'result' after step 2?
AInfinity
B0
CError
Dundefined
💡 Hint
Check the 'Evaluation' and 'Result' columns in row 2 of the execution_table.
At which step does the catch block run in this example?
AStep 3
BNever
CStep 4
DStep 2
💡 Hint
Look at the 'Result' column in step 4; it says 'Catch block skipped'.
If the code inside try threw an error, what would happen?
ATry block continues after error
BProgram crashes immediately
CCatch block runs to handle the error
DFinally block is skipped
💡 Hint
Refer to the concept_flow diagram showing error jumps from try to catch.
Concept Snapshot
try {
  // code that might cause error
} catch (error) {
  // code to handle error
}

- Code in try runs first
- If error occurs, catch runs
- No error means catch skipped
- Finally block can run always (optional)
Full Transcript
This example shows how JavaScript handles runtime errors using try and catch. The program tries to run code inside the try block. If an error happens, it jumps to the catch block to handle it. In this example, dividing 10 by 0 does not cause an error but returns Infinity, so the catch block does not run. The execution table shows each step: starting try, calculating division, printing result, and ending try without error. Variables like 'result' change from undefined to Infinity. Key points include understanding that not all errors throw exceptions (like division by zero in JS) and that catch only runs if an error occurs. The quiz asks about variable values, when catch runs, and what happens if an error occurs. The quick snapshot summarizes the try-catch syntax and behavior.