1
00:00:00,040 --> 00:00:02,080
So let's go into the PowerShell console.

2
00:00:02,080 --> 00:00:04,930
We'll look at generating some errors and exceptions,

3
00:00:04,930 --> 00:00:08,740
and then we'll look at how to handle those errors and exceptions.

4
00:00:08,740 --> 00:00:12,740
One of the downsides of using any kind of programming construct is,

5
00:00:12,740 --> 00:00:16,780
of course, getting errors in your script that you're running.

6
00:00:16,780 --> 00:00:21,540
So maybe you execute a common command, and it fails because,

7
00:00:21,540 --> 00:00:23,210
for example, like we did previously,

8
00:00:23,210 --> 00:00:26,960
where it was the wrong input value or the wrong type,

9
00:00:26,960 --> 00:00:30,290
and then an error is thrown, which kind of messes up the code.

10
00:00:30,290 --> 00:00:33,550
And so one of the most important things about building PowerShell scripts

11
00:00:33,550 --> 00:00:37,540
is to be able to handle the errors in a graceful way.

12
00:00:37,540 --> 00:00:41,630
Now, one of the options that we have is obviously to do what's called Throw,

13
00:00:41,630 --> 00:00:44,370
which is to throw a specific error.

14
00:00:44,370 --> 00:00:48,150
So what we'll do is we'll just create a kind of

15
00:00:48,150 --> 00:00:51,740
basic function within PowerShell.

16
00:00:51,740 --> 00:00:57,440
So let me just do function, and we'll call it New‑Error.

17
00:00:57,440 --> 00:01:01,610
We'll obviously do kind of brackets here.

18
00:01:01,610 --> 00:01:03,050
Let's do this.

19
00:01:03,050 --> 00:01:12,120
And we'll use the word Throw, and then I'll do a message, "This is an error".

20
00:01:12,120 --> 00:01:12,900
So let's do that.

21
00:01:12,900 --> 00:01:15,840
And then we'll close that like so.

22
00:01:15,840 --> 00:01:20,230
Okay, so now we have a function that's available to us,

23
00:01:20,230 --> 00:01:23,240
so I can say New‑Error, and then instantly when we run it,

24
00:01:23,240 --> 00:01:24,610
it throws that error.

25
00:01:24,610 --> 00:01:28,580
And you can see in PowerShell 7 it highlights that for you.

26
00:01:28,580 --> 00:01:30,490
So it'll come back with an exception error,

27
00:01:30,490 --> 00:01:36,740
and then it will say, this is the line or the object that through that error.

28
00:01:36,740 --> 00:01:40,350
So when we're trying to handle things, you could just do this.

29
00:01:40,350 --> 00:01:45,040
You could say, Throw an Error and literally get rid of it.

30
00:01:45,040 --> 00:01:45,700
Now, of course,

31
00:01:45,700 --> 00:01:51,100
that doesn't really help too much when you're trying to build

32
00:01:51,100 --> 00:01:55,260
something that's a little bit easier to handle and can handle the

33
00:01:55,260 --> 00:01:57,440
user flow and the user interaction.

34
00:01:57,440 --> 00:02:00,150
So another option that we've got is to actually use

35
00:02:00,150 --> 00:02:05,780
something called Write‑Error, which allows me to write an error out.

36
00:02:05,780 --> 00:02:08,060
So if I just do this, Write‑Error,

37
00:02:08,060 --> 00:02:14,240
and then it's got a property which is available to us called ‑Message,

38
00:02:14,240 --> 00:02:21,730
so I'm going to add a message, "This is an error",

39
00:02:21,730 --> 00:02:22,830
so the same message.

40
00:02:22,830 --> 00:02:27,380
And then what we get is this kind of error action that's available to us.

41
00:02:27,380 --> 00:02:30,950
Now before we do that, let me just press Enter here.

42
00:02:30,950 --> 00:02:35,210
Now, this will literally just right the error message and say,

43
00:02:35,210 --> 00:02:36,270
"This is an error".

44
00:02:36,270 --> 00:02:37,840
Nothing spectacular.

45
00:02:37,840 --> 00:02:41,900
But if we're trying to handle that process,

46
00:02:41,900 --> 00:02:45,230
then we can also use what's called ‑ErrorAction ID.

47
00:02:45,230 --> 00:02:50,570
And if I just do this one, we can tab, SilentlyContinue, Stop, Suspend.

48
00:02:50,570 --> 00:02:57,030
So we can flick between what we would like to do when an error is generated.

49
00:02:57,030 --> 00:02:59,900
So if I say Write‑Error ‑Message and Stop,

50
00:02:59,900 --> 00:03:03,100
now, obviously, it's just one line, so it's just going to stop.

51
00:03:03,100 --> 00:03:07,390
This will cause the PowerShell function,

52
00:03:07,390 --> 00:03:11,120
method, whatever it is to actually stop working at that point.

53
00:03:11,120 --> 00:03:13,770
So what does that look like if we tie everything together?

54
00:03:13,770 --> 00:03:17,300
So let's go back and do our New function again, so New‑Error.

55
00:03:17,300 --> 00:03:19,950
We'll repeat the same process as before,

56
00:03:19,950 --> 00:03:23,270
but this time we'll do a little bit of a calculation,

57
00:03:23,270 --> 00:03:26,310
so kind of using some of the things that we've done previously.

58
00:03:26,310 --> 00:03:31,710
So for i, so let's do $i is a variable = 1.

59
00:03:31,710 --> 00:03:34,140
So we're using a for loop here,

60
00:03:34,140 --> 00:03:39,310
and then we'll say as long as the i is less than or equal to 10,

61
00:03:39,310 --> 00:03:42,310
so we're going to do a bit of a count here.

62
00:03:42,310 --> 00:03:44,490
I can then do i and ++.

63
00:03:44,490 --> 00:03:47,420
Okay, so let's do that first bit.

64
00:03:47,420 --> 00:03:50,070
What I'm going to do is write a message to the screen,

65
00:03:50,070 --> 00:03:52,940
so what we've done in the past.

66
00:03:52,940 --> 00:03:58,370
"The current number is, and then, of course,

67
00:03:58,370 --> 00:04:01,660
we've got this variable of i that's being populated,

68
00:04:01,660 --> 00:04:05,830
that we can then render inside this message here,

69
00:04:05,830 --> 00:04:06,700
so we'll do this.

70
00:04:06,700 --> 00:04:09,950
And then we'll go down to the next line,

71
00:04:09,950 --> 00:04:14,800
a nd then what we'll do is we'll throw an error message at this point.

72
00:04:14,800 --> 00:04:21,040
So we'll say, "This is an Error", like so,

73
00:04:21,040 --> 00:04:22,170
go underneath it,

74
00:04:22,170 --> 00:04:27,320
and then we actually want to increment a number tw whatever that would be.

75
00:04:27,320 --> 00:04:33,080
So add that one again, close it out, and then we can close the function out.

76
00:04:33,080 --> 00:04:36,240
So we now have our new function.

77
00:04:36,240 --> 00:04:37,280
So let's just review.

78
00:04:37,280 --> 00:04:38,790
We have a New‑Error function.

79
00:04:38,790 --> 00:04:40,400
It starts with the number 0,

80
00:04:40,400 --> 00:04:43,750
and then it counts from basically 1 to everything that's

81
00:04:43,750 --> 00:04:46,100
less than 10 by adding a number to it,

82
00:04:46,100 --> 00:04:49,410
and then what it will do is it will continue to add and write a message,

83
00:04:49,410 --> 00:04:52,340
and then at some point it's going to throw an error.

84
00:04:52,340 --> 00:04:55,370
So what we'll do is we actually run it straightaway,

85
00:04:55,370 --> 00:04:56,650
and, of course, what happens?

86
00:04:56,650 --> 00:05:01,740
It just says the current number is 1, and then because it threw the error,

87
00:05:01,740 --> 00:05:03,690
it cannot do anything else.

88
00:05:03,690 --> 00:05:08,040
So throwing an error will literally cause everything to stop.

89
00:05:08,040 --> 00:05:10,320
So let's go a little step further,

90
00:05:10,320 --> 00:05:13,800
and we'll take that same function that we just built.

91
00:05:13,800 --> 00:05:16,540
So let me just loop through my commands a little bit here.

92
00:05:16,540 --> 00:05:23,440
So there's my clear command, New‑Error.

93
00:05:23,440 --> 00:05:27,140
Not the Write‑Host.

94
00:05:27,140 --> 00:05:29,390
Let's go number, function.

95
00:05:29,390 --> 00:05:29,850
There we go.

96
00:05:29,850 --> 00:05:34,440
Function number is 0.

97
00:05:34,440 --> 00:05:37,040
We'll go back to the for loop.

98
00:05:37,040 --> 00:05:40,600
We'll then do the same command as before for the Write‑Host.

99
00:05:40,600 --> 00:05:43,940
Now this time we're not going to throw an error.

100
00:05:43,940 --> 00:05:50,540
We're going to say Write‑Error instead, so exactly the same thing.

101
00:05:50,540 --> 00:05:52,330
We'll do the message that we're going to enter,

102
00:05:52,330 --> 00:05:54,840
so we'll say, Write‑Error ‑Message.

103
00:05:54,840 --> 00:05:58,310
And so we want to represent the fact that an error was caused.

104
00:05:58,310 --> 00:06:03,780
And I'll say "Error", just like so, to keep it short and sweet.

105
00:06:03,780 --> 00:06:06,610
But then what I'm going to do is do an ‑ErrorAction.

106
00:06:06,610 --> 00:06:17,100
Oops, ‑ErrorAction, like so, and I'm going to say SilentlyContinue.

107
00:06:17,100 --> 00:06:19,440
Do Enter.

108
00:06:19,440 --> 00:06:23,890
And then, of course, I need to add my next line,

109
00:06:23,890 --> 00:06:26,400
which is, if I just go back here, function.

110
00:06:26,400 --> 00:06:27,400
I'll tell you what.

111
00:06:27,400 --> 00:06:36,740
We'll just write that one out, which will be $number += $i,

112
00:06:36,740 --> 00:06:40,340
and then we'll close and close.

113
00:06:40,340 --> 00:06:42,500
So, now I have my function updated.

114
00:06:42,500 --> 00:06:45,060
So before we call it, let's just double check.

115
00:06:45,060 --> 00:06:45,980
Same thing again.

116
00:06:45,980 --> 00:06:50,360
Start with a 0, iterate from 1 to 10, checking that it's less than 10.

117
00:06:50,360 --> 00:06:53,760
It will write a message saying the current number is something.

118
00:06:53,760 --> 00:06:58,820
Then it will write an error, but this time it should silently continue.

119
00:06:58,820 --> 00:07:00,240
So let's just press Enter.

120
00:07:00,240 --> 00:07:01,150
And sure enough, it does.

121
00:07:01,150 --> 00:07:03,840
Now notice it didn't render anything.

122
00:07:03,840 --> 00:07:09,040
It just silently continued and finished all the way to number 10.

123
00:07:09,040 --> 00:07:10,400
So we captured the error.

124
00:07:10,400 --> 00:07:13,140
We effectively grabbed it, and we could have done something.

125
00:07:13,140 --> 00:07:18,140
I chose to write an error, but of course, I told it to silently continue also.

126
00:07:18,140 --> 00:07:23,820
So if we go back and if we go looping through it pretty quickly here,

127
00:07:23,820 --> 00:07:32,490
function, number, for loop, Write‑Host, and change it from SilentlyContinue to,

128
00:07:32,490 --> 00:07:39,740
let's do Suspend, and then let me get rid of this here.

129
00:07:39,740 --> 00:07:44,420
I have to obviously do my $number += $i.

130
00:07:44,420 --> 00:07:54,590
And then we'll close that one down,

131
00:07:54,590 --> 00:07:57,090
and then we'll close the next line down as well.

132
00:07:57,090 --> 00:08:02,220
And so that should then give us our new function, ready to call again.

133
00:08:02,220 --> 00:08:07,860
And this time around we're telling it to cause a suspension of the code,

134
00:08:07,860 --> 00:08:09,460
so I'm going to say Error.

135
00:08:09,460 --> 00:08:11,540
Now, of course, note just what happened.

136
00:08:11,540 --> 00:08:13,960
If you come all the way through here, we can keep scrolling.

137
00:08:13,960 --> 00:08:16,020
It's moaning about a specific action.

138
00:08:16,020 --> 00:08:19,180
You can see that it's looping, and it's trying to do something.

139
00:08:19,180 --> 00:08:20,970
If we look at the message, it says,

140
00:08:20,970 --> 00:08:25,000
The Suspend ActionPreference is reserved for future use.

141
00:08:25,000 --> 00:08:26,110
Perfect.

142
00:08:26,110 --> 00:08:28,770
It's always good to know that when you're trying to use values,

143
00:08:28,770 --> 00:08:32,240
you need to make sure that just because something is documented,

144
00:08:32,240 --> 00:08:34,610
you need to make sure that it's actually useful.

145
00:08:34,610 --> 00:08:39,950
So the effective options here would be to stop or to

146
00:08:39,950 --> 00:08:41,710
actually just silently continue.

147
00:08:41,710 --> 00:08:48,050
So if we do that one more time, number, for loop here,

148
00:08:48,050 --> 00:08:55,620
I'm going to say Stop, and then I'll do my $number += $i,

149
00:08:55,620 --> 00:09:00,370
that one, that one, and that one, and then do New‑Error,

150
00:09:00,370 --> 00:09:04,840
you can see it will just get to number 1 and error.

151
00:09:04,840 --> 00:09:08,020
So it's a simple way of capturing it, a way of trying to handle it,

152
00:09:08,020 --> 00:09:10,800
just rendering a message or breaking or stopping.

153
00:09:10,800 --> 00:09:12,630
It's not the most ideal way of doing it,

154
00:09:12,630 --> 00:09:17,470
but it does allow you to kind of at least when you break and there's an error,

155
00:09:17,470 --> 00:09:20,440
then, of course, you can capture what that error would be.

156
00:09:20,440 --> 00:09:22,630
And, of course, whenever you throw the error,

157
00:09:22,630 --> 00:09:27,250
you can also capture more information about what that message would be.

158
00:09:27,250 --> 00:09:32,520
So if I say Throw "Error", like so, it will say Exception.

159
00:09:32,520 --> 00:09:41,740
But if I say $, let's create a variable here and call it Throw "Error",

160
00:09:41,740 --> 00:09:45,290
like so, now if I do the error,

161
00:09:45,290 --> 00:09:50,460
you can see it gives me a whole host of information that's associated to

162
00:09:50,460 --> 00:09:53,130
that and everything around what the error would be.

163
00:09:53,130 --> 00:09:56,930
So I can say $error., and if I tab through it,

164
00:09:56,930 --> 00:10:00,340
you can see I get the regular properties that are available.

165
00:10:00,340 --> 00:10:02,600
But of course, depending on the name of the variable,

166
00:10:02,600 --> 00:10:05,550
I can also get access to all of the other values too.

167
00:10:05,550 --> 00:10:08,120
Now, I will give you a hint here.

168
00:10:08,120 --> 00:10:10,930
Try not to use kind of the same name as maybe some

169
00:10:10,930 --> 00:10:13,260
of the default values would be, you know,

170
00:10:13,260 --> 00:10:16,220
especially when you're using things like the word error or something else.

171
00:10:16,220 --> 00:10:20,040
They are often reserved words, such as PSItem and things like that.

172
00:10:20,040 --> 00:10:22,170
So you can see I'm just scrolling all the way back through, and

173
00:10:22,170 --> 00:10:25,170
you'll see it's a whole host of different errors and messages that

174
00:10:25,170 --> 00:10:28,140
get rendered from various different things.

175
00:10:28,140 --> 00:10:35,000
So you have the ability to not only throw an error, but to obviously capture the error details.

