Javascript - try...catch Blocks
Code Life
By Pierre Marsaa
Catching a Crash Before It Stops the Program
By default, when a runtime error happens, JavaScript stops the entire program immediately. Any line of code after the crash never gets a chance to run:
↗️
Loading editor...
↶
↷
Because entries[1] is undefined, line 4 crashes — and "Aquachu" on line 5 is never printed!
A try...catch block fixes that. You place the risky code inside try. If it crashes, JavaScript jumps straight into catch, runs whatever is inside it, and then continues with the rest of the program:
↗️
Loading editor...
↶
↷
This time, "Aquachu" still prints! The crash was caught, handled with a message, and the program survived.
Verify