When we write code, the computer might have trouble following our instructions.
If we make a mistake in our code, or if something unexpected happens when our code is running, we could get an error.
When an error happens, the code stops running.
Run this example code:
Types of Errors
The code above throws this error:
SyntaxError: Unexpected number [solution.js:1:3]
Errors often have helpful messages that point to where the error happened.
This one tells us several things:
- the error was caused by an unexpected number
- the error happened in the solution.js file (meaning the practice editor)
- the error happened on line 1, column 3
7 created this error.
There are two numbers but no operators in this code. We aren't adding or subtracting or doing anything else.
There are ways to deal with errors in code and keep the program running.
You can use something called a try...catch statement to "catch" errors and handle with them.
However, if possible we should always write code that doesn't cause errors. Errors help us find problems and fix our code.
None of the lessons in this course require you to use try...catch statements.