1
00:00:02,210 --> 00:00:07,940
Now after all these if statements and all these loops, I want to conclude this module by diving into

2
00:00:08,090 --> 00:00:14,330
error handling, another form of controlled structure you'll need in your Javascript code to build code

3
00:00:14,360 --> 00:00:20,220
that runs under different and also under harder circumstances.

4
00:00:20,300 --> 00:00:21,630
Now what do I mean by that?

5
00:00:21,890 --> 00:00:26,000
In the Javascript applications, in the programs you're going to write,

6
00:00:26,000 --> 00:00:31,220
you can't always avoid all errors. Some errors can't be avoided

7
00:00:31,220 --> 00:00:35,580
and with that I of course mean errors that are beyond your control as a developer.

8
00:00:35,660 --> 00:00:41,390
So I'm not talking about bugs which you introduced into your code or about syntax errors,

9
00:00:41,390 --> 00:00:46,040
you should have a look at the debugging module in this course to learn how you can fix and avoid

10
00:00:46,040 --> 00:00:47,260
such errors,

11
00:00:47,270 --> 00:00:52,280
I'm talking about errors which you can't rule out when you're writing your code,

12
00:00:52,280 --> 00:01:00,440
for example user input errors, that a user enters text like hi in a field where you expect a number

13
00:01:00,920 --> 00:01:06,530
or network errors, that you're maybe communicating with some server, something we haven't done yet but

14
00:01:06,530 --> 00:01:13,520
which we'll do later down in the course and that server is offline or something like that,

15
00:01:13,520 --> 00:01:20,750
so basically things where you have no chance of ruling out that this problem occurs because you can't

16
00:01:20,750 --> 00:01:24,230
control what a user types into the input field.

17
00:01:24,350 --> 00:01:31,100
You can do your best to kind of restrict the options to user has but ultimately, users might always find

18
00:01:31,100 --> 00:01:38,420
a way of breaking your code so to say and the same for network errors, you can't rule out that a server

19
00:01:38,420 --> 00:01:41,830
might be offline when you're trying to talk to it.

20
00:01:41,840 --> 00:01:49,490
So the best you can do in these cases is that you throw your own errors and catch errors to fail gracefully,

21
00:01:49,490 --> 00:01:56,060
which means maybe at least show the user a nice error message or in the best possible case, maybe you

22
00:01:56,060 --> 00:02:02,060
can write some fallback code so that you can recover and you don't even need to show an error message

23
00:02:02,060 --> 00:02:09,530
to the user and you can do that with try catch, another control structure which Javascript exposes to

24
00:02:09,530 --> 00:02:17,240
you that allows you to try a certain code which might fail and then catch potential errors thrown in

25
00:02:17,240 --> 00:02:22,850
that code to then do something with them or to do something indicates that such an error occurred.
