1
00:00:00,240 --> 00:00:04,840
So let's go back into the PowerShell console and we'll use the

2
00:00:04,840 --> 00:00:09,040
TRY/CATCH syntax to capture the errors and exceptions, and then

3
00:00:09,040 --> 00:00:12,910
we'll enhance that to use the TRY/CATCH/FINALLY syntax to

4
00:00:12,910 --> 00:00:14,850
capture errors and exceptions.

5
00:00:14,850 --> 00:00:19,540
So one of the ways that we can capture errors outside of

6
00:00:19,540 --> 00:00:23,280
using error action or throwing errors is to actually try and

7
00:00:23,280 --> 00:00:24,780
catch them in the first place.

8
00:00:24,780 --> 00:00:28,490
So let's just go ahead and create the function that we had before, so

9
00:00:28,490 --> 00:00:33,340
New‑Error, and then I'm going to throw an error here,

10
00:00:33,340 --> 00:00:42,120
so Throw New Error, like so. And so we have our function, so I'm going to say

11
00:00:42,120 --> 00:00:48,300
New‑Error, Enter, and what it should do is it should throw my error. Perfect,

12
00:00:48,300 --> 00:00:51,440
nothing spectacular, it just throws the error.

13
00:00:51,440 --> 00:00:54,370
Now, of course, if you have that in the code,

14
00:00:54,370 --> 00:00:55,520
then, of course, unfortunately,

15
00:00:55,520 --> 00:00:58,150
what's going to happen is when it executes the throw,

16
00:00:58,150 --> 00:01:01,740
it will cause the code to stop executing.

17
00:01:01,740 --> 00:01:07,540
So what we can do here is wrap things inside a try and a catch.

18
00:01:07,540 --> 00:01:07,750
Now,

19
00:01:07,750 --> 00:01:12,390
the idea behind the syntax here is that we can try whatever it is we want to do,

20
00:01:12,390 --> 00:01:15,850
and then we can catch the output of that.

21
00:01:15,850 --> 00:01:17,110
So I'm going to say,

22
00:01:17,110 --> 00:01:24,810
Write‑Host and say An Error Occurred, and then close that. Now when

23
00:01:24,810 --> 00:01:29,920
I Enter this, it will now not display the throw message, but will

24
00:01:29,920 --> 00:01:34,930
display the message or perform the code action that we actually

25
00:01:34,930 --> 00:01:36,600
asked it to in the first place.

26
00:01:36,600 --> 00:01:40,440
So it's a nice way of saying, well, if we have functions that throw errors,

27
00:01:40,440 --> 00:01:44,540
we can catch them gracefully and then have them do something else.

28
00:01:44,540 --> 00:01:47,370
Now, we can look at this a little bit further because we can

29
00:01:47,370 --> 00:01:52,590
throw specific types of errors as well.

30
00:01:52,590 --> 00:01:54,450
So if I say New‑Error,

31
00:01:54,450 --> 00:01:58,200
so we have our standard function, and then this time I'm going to say

32
00:01:58,200 --> 00:02:01,950
throw, and in square brackets here I can say

33
00:02:01,950 --> 00:02:11,840
System.IO.FileNotFoundException, and then just say File not found,

34
00:02:11,840 --> 00:02:17,840
like so, Enter that one, and then close that function.

35
00:02:17,840 --> 00:02:21,240
Now when I execute this New‑Error,

36
00:02:21,240 --> 00:02:25,340
it will now throw a type of error called

37
00:02:25,340 --> 00:02:29,300
System.IO.FileNotFoundException and render the message.

38
00:02:29,300 --> 00:02:30,970
Now, of course, I suppose the question is,

39
00:02:30,970 --> 00:02:33,450
how do we know that it actually rendered that

40
00:02:33,450 --> 00:02:36,180
specific message or specific error,

41
00:02:36,180 --> 00:02:42,150
which was the File not found? So let's go back and take that same one that

42
00:02:42,150 --> 00:02:46,900
we've just generated, and we'll wrap it into a try statement.

43
00:02:46,900 --> 00:02:51,790
So we're going to say try, and then I'm going to say let's execute my

44
00:02:51,790 --> 00:02:56,580
New‑Error, and then I'm going to do this, and then go down and say,

45
00:02:56,580 --> 00:03:01,640
I want to catch a specific message. And what I can do here is

46
00:03:01,640 --> 00:03:05,340
say Write‑Host, and remember we talked about using the dollar

47
00:03:05,340 --> 00:03:09,300
and then the underscore as one entry,

48
00:03:09,300 --> 00:03:19,430
I'm then going to say $_. I should be able to then get the type of the catch

49
00:03:19,430 --> 00:03:25,440
that's come through, so I can say GetType, and say go and get me the FullName.

50
00:03:25,440 --> 00:03:31,500
I can then use that $_., and then I have what's called Exception, so I can get

51
00:03:31,500 --> 00:03:43,230
that value too. I can do $_.Exception.GetType.FullName, so this should give me

52
00:03:43,230 --> 00:03:45,040
the full name of the exception.

53
00:03:45,040 --> 00:03:47,940
And then, to top it all off,

54
00:03:47,940 --> 00:03:53,690
I can say Exception.Message, and then I can close that out

55
00:03:53,690 --> 00:03:58,900
like so. Now notice what happens. We now get a full set of

56
00:03:58,900 --> 00:03:59,930
information coming back.

57
00:03:59,930 --> 00:04:02,750
Now, bear in mind, I don't have much information that came back,

58
00:04:02,750 --> 00:04:05,780
but you can see that it shows the message, it says there was

59
00:04:05,780 --> 00:04:09,710
an error, it has System.IO.FileNotFound,

60
00:04:09,710 --> 00:04:12,530
which was the type that we kind of had, and then,

61
00:04:12,530 --> 00:04:13,910
of course, it has the message.

62
00:04:13,910 --> 00:04:17,960
So all of this information is captured whenever we

63
00:04:17,960 --> 00:04:20,470
try to use the try and the catch,

64
00:04:20,470 --> 00:04:26,740
and especially if we're trying to catch specific types of errors as well.

65
00:04:26,740 --> 00:04:29,280
So what we can do is go a little bit further here,

66
00:04:29,280 --> 00:04:33,380
where we're able to catch specific types of errors as well.

67
00:04:33,380 --> 00:04:36,440
So right now if we just scroll back up here,

68
00:04:36,440 --> 00:04:40,390
let's just move back up here, you can see that I simply just said try, run

69
00:04:40,390 --> 00:04:44,370
the New‑Error, and then catch whatever was there. Now,

70
00:04:44,370 --> 00:04:45,840
in reality,

71
00:04:45,840 --> 00:04:51,480
I may know what the types of errors are that I want to kind of capture,

72
00:04:51,480 --> 00:04:55,640
I suppose, so what we can do, and we'll just do the syntax for this, I

73
00:04:55,640 --> 00:05:00,880
can say try New‑Error, run this one here,

74
00:05:00,880 --> 00:05:05,330
say catch, and then at this point in square brackets I can then

75
00:05:05,330 --> 00:05:10,420
determine the type or the strong type, I suppose,

76
00:05:10,420 --> 00:05:17,790
of error that we wish to handle. So I can say FileNot, so

77
00:05:17,790 --> 00:05:33,500
it should be FileNotFoundException, Exception is the first

78
00:05:33,500 --> 00:05:37,140
one that we're going to catch,

79
00:05:37,140 --> 00:05:47,540
and then I obviously have what I would like it to do, so Write‑Host Catch.

80
00:05:47,540 --> 00:05:48,220
And then, of course,

81
00:05:48,220 --> 00:05:52,690
we can go down and start adding another catch statement if I wanted

82
00:05:52,690 --> 00:06:01,970
to, and go through and say System.IO.IOException,

83
00:06:01,970 --> 00:06:05,040
which is one that's rendered when you try to open. And then we can go

84
00:06:05,040 --> 00:06:07,270
and complete and keep adding, and adding, and adding.

85
00:06:07,270 --> 00:06:11,770
So what we actually have here is the ability to go through and capture

86
00:06:11,770 --> 00:06:18,940
specific error types and output the values that we would need to.

87
00:06:18,940 --> 00:06:19,270
Okay,

88
00:06:19,270 --> 00:06:22,630
so let's look at creating something a bit more real

89
00:06:22,630 --> 00:06:24,840
here. So let's clear this out.

90
00:06:24,840 --> 00:06:28,310
I'm actually going to paste in a specific function,

91
00:06:28,310 --> 00:06:33,090
and then we'll look at the code itself. So you can see I've pasted it in.

92
00:06:33,090 --> 00:06:35,550
It's the same function, so New‑Error,

93
00:06:35,550 --> 00:06:39,500
and this time I've combined it with a string value so I can pass a letter A,

94
00:06:39,500 --> 00:06:45,200
B, and C to a switch statement, and then I'm telling it to throw a specific

95
00:06:45,200 --> 00:06:48,230
type of exception, so that's going to be our function.

96
00:06:48,230 --> 00:06:50,310
So what does that look like when we execute it?

97
00:06:50,310 --> 00:06:50,670
Well,

98
00:06:50,670 --> 00:06:57,470
if we say New‑Error, and then you'll see ‑type and I can say A, you can

99
00:06:57,470 --> 00:07:00,590
see it will throw a System FileNotFoundException,

100
00:07:00,590 --> 00:07:01,590
which is correct.

101
00:07:01,590 --> 00:07:04,150
If I rerun that and say B,

102
00:07:04,150 --> 00:07:08,780
it should give me a System IOException, and if I run C, it

103
00:07:08,780 --> 00:07:10,470
should give me the System Exception.

104
00:07:10,470 --> 00:07:12,940
So we just have a function that's going to generate

105
00:07:12,940 --> 00:07:17,440
specific typed errors and throw them.

106
00:07:17,440 --> 00:07:19,170
Now, how do we capture this?

107
00:07:19,170 --> 00:07:23,140
Well, let me copy some more and paste that in.

108
00:07:23,140 --> 00:07:23,960
So here it is.

109
00:07:23,960 --> 00:07:28,550
We're doing a try statement and saying New‑Error and passing the letter A, so we

110
00:07:28,550 --> 00:07:31,730
know that A is going to be a System FileNotFoundException.

111
00:07:31,730 --> 00:07:35,730
It will catch that based on the type, and then

112
00:07:35,730 --> 00:07:38,100
represent the message that's here.

113
00:07:38,100 --> 00:07:38,750
And then, of course,

114
00:07:38,750 --> 00:07:42,280
we have one for each of the options that we've added into there.

115
00:07:42,280 --> 00:07:46,730
So I'm going to do just Enter here, and it should come back

116
00:07:46,730 --> 00:07:49,600
and say Got It: File Not Found Exception, and then it should

117
00:07:49,600 --> 00:07:52,740
always render the finally option.

118
00:07:52,740 --> 00:07:57,520
So what we can do now is go back here, and if I wanted to change that,

119
00:07:57,520 --> 00:07:58,760
I could say, well,

120
00:07:58,760 --> 00:08:01,900
New‑Error, so let me just go back through each of

121
00:08:01,900 --> 00:08:04,490
the items and we'll do that one, that one,

122
00:08:04,490 --> 00:08:05,190
that one,

123
00:08:05,190 --> 00:08:10,030
that one, that one, and Enter it, and you can see it's now gone to the IO

124
00:08:10,030 --> 00:08:14,620
Exception because we told it to generate that specific error, and so it's

125
00:08:14,620 --> 00:08:17,640
caught the error and then has passed the value.

126
00:08:17,640 --> 00:08:18,700
So, as you can see,

127
00:08:18,700 --> 00:08:22,170
it's not the easiest thing, but as long as you understand

128
00:08:22,170 --> 00:08:23,830
the errors that you're coming back with,

129
00:08:23,830 --> 00:08:28,440
you can capture those and then redirect or transfer to other

130
00:08:28,440 --> 00:08:32,540
functions or simply output messages to the end user.

131
00:08:32,540 --> 00:08:37,070
So it's important to understand that you have this idea of throwing messages,

132
00:08:37,070 --> 00:08:45,000
throwing errors or writing messages or exceptions, but in reality, you can catch those and make it do what you need it to do.

