0
0
Javascriptprogramming~10 mins

Try–catch block in Javascript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Try–catch block
Start
Try block runs
Error?
NoTry block ends normally
|Yes
Catch block runs
Continue after catch
End
The program tries to run code inside the try block. If an error happens, it jumps to the catch block to handle it, then 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 print the result. If an error occurs, it catches and prints the error message.
Execution Table
StepActionEvaluationResult
1Enter try blockNo error yetContinue
2Calculate 10 / 0Division by zero in JS returns Infinityresult = Infinity
3console.log('Result:', result)Prints 'Result: Infinity'Output: Result: Infinity
4Try block endsNo error thrownSkip catch block
5Program continues after try-catchNormal flowEnd
💡 Try block completed without error, so catch block is skipped.
Variable Tracker
VariableStartAfter Step 2Final
resultundefinedInfinityInfinity
errorundefinedundefinedundefined
Key Moments - 2 Insights
Why doesn't the catch block run when dividing by zero?
In JavaScript, dividing by zero does not throw an error; it returns Infinity. So the try block finishes normally and the catch block is skipped, as shown in execution_table step 4.
What happens if an error is thrown inside the try block?
If an error occurs, execution immediately jumps to the catch block to handle it. This is not shown here but would appear as a jump from try to catch in the 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
BStep 4
CNever
DStep 5
💡 Hint
Look at the 'Result' column in step 4; it says 'Skip catch block'.
If the code inside try threw an error, what would happen next?
AThe program would crash immediately
BThe catch block would run to handle the error
CThe try block would continue running
DNothing changes, catch block is ignored
💡 Hint
Refer to the concept_flow where error leads to catch block execution.
Concept Snapshot
try {
  // code that might fail
} catch (error) {
  // code to handle error
}

- Runs try block code
- If error occurs, jumps to catch
- catch receives error object
- After catch, program continues normally
Full Transcript
A try-catch block lets your program try to run code that might cause an error. If an error happens, the program jumps to the catch block to handle it without crashing. In JavaScript, some things like dividing by zero do not cause errors but return special values like Infinity, so catch does not run. The try block runs first, and if no error occurs, the catch block is skipped. If an error happens, the catch block runs with the error details. After catch, the program continues normally.