1
00:00:00,040 --> 00:00:03,780
Now the most important way that people kind of catch these errors

2
00:00:03,780 --> 00:00:07,260
is by using what's referred to as the TRY/CATCH,

3
00:00:07,260 --> 00:00:10,440
TRY/CATCH/FINALLY statements.

4
00:00:10,440 --> 00:00:15,600
Now, of course, there are three of these, so the first one is a TRY/CATCH.

5
00:00:15,600 --> 00:00:20,150
This will try the section of code that you wish to execute,

6
00:00:20,150 --> 00:00:21,730
and if it throws an error,

7
00:00:21,730 --> 00:00:25,940
it will drop it into a catch where you can do something else.

8
00:00:25,940 --> 00:00:29,590
The TRY/FINALLY doesn't handle the error.

9
00:00:29,590 --> 00:00:33,910
It simply executes the code if the exception occurs.

10
00:00:33,910 --> 00:00:39,850
The TRY/CATCH/FINALLY will be the combination of throwing errors and

11
00:00:39,850 --> 00:00:43,400
executing code. So you don't have to use all of them.

12
00:00:43,400 --> 00:00:47,070
You can pick whichever one works for the script that you're

13
00:00:47,070 --> 00:00:50,470
writing. So if I want to generate an exception,

14
00:00:50,470 --> 00:00:51,990
I'm going to use our standard function.

15
00:00:51,990 --> 00:00:55,860
So a standard function of New‑Error Throw "This is an Error."

16
00:00:55,860 --> 00:00:59,040
That will cause me to have an exception.

17
00:00:59,040 --> 00:01:02,230
If I was to wrap this around in a TRY/CATCH,

18
00:01:02,230 --> 00:01:05,590
let's say I was calling that function, I could say try

19
00:01:05,590 --> 00:01:10,540
New‑Error, and if the new error caused an issue,

20
00:01:10,540 --> 00:01:11,970
I could then catch that,

21
00:01:11,970 --> 00:01:14,930
which is the next line down, and then it would write a

22
00:01:14,930 --> 00:01:17,840
value saying An Exception was Generated.

23
00:01:17,840 --> 00:01:21,820
So the try and the catch are very similar syntax to

24
00:01:21,820 --> 00:01:24,280
the loops that we were looking at, where we're,

25
00:01:24,280 --> 00:01:24,780
you know,

26
00:01:24,780 --> 00:01:28,490
saying an if and else or we're looping through something and saying

27
00:01:28,490 --> 00:01:32,490
if this else if else, or the same as a switch statement, which would

28
00:01:32,490 --> 00:01:37,660
be switch conditions and then a default. It's the same logic here. So

29
00:01:37,660 --> 00:01:39,940
we test something in the try.

30
00:01:39,940 --> 00:01:43,240
If it fails, we catch.

31
00:01:43,240 --> 00:01:46,150
If we were to change this to use the FINALLY,

32
00:01:46,150 --> 00:01:50,720
then what you can see is we have try and finally, it will go and

33
00:01:50,720 --> 00:01:52,850
say I'm using a function called New‑Message,

34
00:01:52,850 --> 00:01:55,440
which is going to write a message out, and it will say

35
00:01:55,440 --> 00:01:58,240
New‑Message finally Write‑Output.

36
00:01:58,240 --> 00:02:00,550
So if it continues to error,

37
00:02:00,550 --> 00:02:03,290
it will just capture the error but then drop down

38
00:02:03,290 --> 00:02:05,840
and carry on and execute the code.

39
00:02:05,840 --> 00:02:11,060
Now we could mix this all together and use the TRY/CATCH with the

40
00:02:11,060 --> 00:02:13,890
FINALLY. So you can see I'm calling my test,

41
00:02:13,890 --> 00:02:16,880
which is my New‑Message, which will have my throw code in it.

42
00:02:16,880 --> 00:02:19,630
It's going to throw the message an error.

43
00:02:19,630 --> 00:02:20,920
I'm then going to catch it,

44
00:02:20,920 --> 00:02:25,610
and it will write the value out. Now, depending on the logic here,

45
00:02:25,610 --> 00:02:31,040
it could either go to the catch or it would drop out to the finally.

46
00:02:31,040 --> 00:02:33,190
So depending on how you structure this,

47
00:02:33,190 --> 00:02:37,370
you can either catch it or you can go to the finally and write the message out.

48
00:02:37,370 --> 00:02:41,140
But we'll look at this one in a few minutes.

49
00:02:41,140 --> 00:02:45,450
We also have what's called typed exception handling available to us.

50
00:02:45,450 --> 00:02:50,790
So exceptions that have a specific type, you can specify the type of

51
00:02:50,790 --> 00:02:53,340
exception that you're trying to catch.

52
00:02:53,340 --> 00:02:55,620
So if it's a piece of string value,

53
00:02:55,620 --> 00:02:59,540
then you can say, This is the type of exception I wish you to catch.

54
00:02:59,540 --> 00:03:05,240
You can catch multiple exception types with a single TRY/CATCH statement.

55
00:03:05,240 --> 00:03:07,290
So what does that look like in reality?

56
00:03:07,290 --> 00:03:11,540
So I have a path here, so C:\Documents\Code.

57
00:03:11,540 --> 00:03:17,050
I do a try, and the try says New‑Error ‑Path, ‑ErrorAction Stop.

58
00:03:17,050 --> 00:03:19,440
So that's my initial try.

59
00:03:19,440 --> 00:03:20,800
If I go underneath that,

60
00:03:20,800 --> 00:03:23,900
I can then use the catch statement here, and notice what I'm

61
00:03:23,900 --> 00:03:27,830
doing. Instead of it just catching the error, I'm looking

62
00:03:27,830 --> 00:03:30,630
for specific types of errors.

63
00:03:30,630 --> 00:03:35,740
So I'm looking for DirectoryNotFound or FileNotFound,

64
00:03:35,740 --> 00:03:37,940
and then I'm going to write a value.

65
00:03:37,940 --> 00:03:42,930
If I go to the catch, I could also do just an IOException so it couldn't

66
00:03:42,930 --> 00:03:47,500
open the file. So we can now strongly type the catch.

67
00:03:47,500 --> 00:03:54,000
So instead of it catching every exception, it's only going to catch it if it matches that strongly typed configuration.

