(9/10)
Handling Errors (Try-Catch) — JavaScript typing lesson
Learn try/catch to handle errors gracefully.
What you'll practice
What if a user types text when we expect a number? Our program could crash! Let's learn to handle errors with `try` and `catch`.
- JavaScript will `try` to run the code inside the `try` block.
- If an error happens in the `try` block, the code in the `catch` block will run instead.
Pro Access Required
This is a premium lesson. Upgrade to Pro to unlock this interactive exercise and continue your learning journey.
📜JavaScript
Handling Errors (Try-Catch)
0 WPM
0% Accuracy
0:00 Time elapsed
1
2
3
4
5
6
7
8
let number_as_text = prompt('give me a number');↵
↵
try {↵
let number = parseInt(number_as_text);↵
console.log('your number doubled is ' + (number * 2));↵
} catch (error) {↵
console.log('whoops that was not a number');↵
}
0 / 226
Press This!
👇
`~
1!
2@
3#
4$
5%
6^
7&
8*
9(
0)
-_
=+
⌫
⇥
Q
W
E
R
T
Y
U
I
O
P
[{
]}
\|
⇪
A
S
D
F
G
H
J
K
;:
'"
↵
⇧
Z
X
C
V
B
N
M
,<
.>
/?
⇧
Thumb
Index
Middle
Ring
Pinky