1
00:00:00,340 --> 00:00:04,540
So let's first understand errors and exceptions.

2
00:00:04,540 --> 00:00:08,890
So let's understand the basic terminology. So the first one is an exception.

3
00:00:08,890 --> 00:00:10,330
This is like an event.

4
00:00:10,330 --> 00:00:16,440
It gets created when a normal error handling can't deal with the issue.

5
00:00:16,440 --> 00:00:20,170
The second one is throw and catch. So this happens when an

6
00:00:20,170 --> 00:00:23,850
exception occurs, and so you say that an exception is

7
00:00:23,850 --> 00:00:26,770
thrown. To handle a thrown exception,

8
00:00:26,770 --> 00:00:30,840
you have to catch that exception to do something with it.

9
00:00:30,840 --> 00:00:32,650
Now, along with that is the call stack.

10
00:00:32,650 --> 00:00:36,740
The call stack is the list of functions that were called by each

11
00:00:36,740 --> 00:00:41,140
other that led up to and after the exception.

12
00:00:41,140 --> 00:00:45,510
We then have what's called terminating and non‑terminating errors.

13
00:00:45,510 --> 00:00:49,040
This is an exception that's generally a terminating error.

14
00:00:49,040 --> 00:00:54,540
A thrown exception can either be caught or it terminates the current execution.

15
00:00:54,540 --> 00:00:57,040
And then we have what's called swallowing an exception.

16
00:00:57,040 --> 00:01:01,890
So this is when you catch an error and it just literally disappears.

17
00:01:01,890 --> 00:01:03,760
So it doesn't really serve any purpose,

18
00:01:03,760 --> 00:01:06,640
but you're just capturing it and ignoring it.

19
00:01:06,640 --> 00:01:10,340
So what are the differences between errors and exceptions?

20
00:01:10,340 --> 00:01:14,640
Well, an error is returned as a PowerShell object,

21
00:01:14,640 --> 00:01:20,640
and so it provides the terminating and also non‑terminating errors,

22
00:01:20,640 --> 00:01:24,090
whereas exceptions are created when the normal error

23
00:01:24,090 --> 00:01:27,150
handling cannot handle the issue.

24
00:01:27,150 --> 00:01:32,940
Exceptions are typically non‑terminating errors, effectively.

25
00:01:32,940 --> 00:01:37,080
So what's the difference between a terminating and a non‑terminating? Well,

26
00:01:37,080 --> 00:01:40,630
a terminating one, the error is generated by the script,

27
00:01:40,630 --> 00:01:42,370
function, or commands.

28
00:01:42,370 --> 00:01:47,900
It will officially stop or halt the execution going forward. Whereas a

29
00:01:47,900 --> 00:01:52,700
non‑terminating one, this tends to be generated by internal commands.

30
00:01:52,700 --> 00:01:56,440
It's normally automatically handled so the error doesn't

31
00:01:56,440 --> 00:02:01,340
terminate the execution of the pipeline.

32
00:02:01,340 --> 00:02:05,140
So what are the error handling approaches that we can use?

33
00:02:05,140 --> 00:02:07,430
Well, the first one is to throw the exception.

34
00:02:07,430 --> 00:02:09,040
So as soon as the exception comes,

35
00:02:09,040 --> 00:02:11,090
we capture it, and we just throw the exception,

36
00:02:11,090 --> 00:02:16,040
which effectively will potentially break and stop the execution.

37
00:02:16,040 --> 00:02:17,860
We can do what's called Write‑Error,

38
00:02:17,860 --> 00:02:21,540
so we effectively write a message out and say we found

39
00:02:21,540 --> 00:02:24,540
an error and write the message out.

40
00:02:24,540 --> 00:02:26,980
We could also use what's called ‑ErrorAction,

41
00:02:26,980 --> 00:02:30,000
which is an operator that will then determine what we can do.

42
00:02:30,000 --> 00:02:34,040
There's some predetermined values for the error action.

43
00:02:34,040 --> 00:02:37,620
We could also Try/Catch, the idea being that as it

44
00:02:37,620 --> 00:02:43,840
executes it will try, and if it errors, then we automatically catch that error.

45
00:02:43,840 --> 00:02:46,840
We could then also do what's called Try and Finally,

46
00:02:46,840 --> 00:02:50,970
which is a combination of the Try and the Catch with a default

47
00:02:50,970 --> 00:02:53,840
of Finally action that comes out of there.

48
00:02:53,840 --> 00:02:56,600
And then, of course, we can do Try/Catch/Finally,

49
00:02:56,600 --> 00:02:58,910
So it's really up to you the different options that we've

50
00:02:58,910 --> 00:03:02,140
got, but we'll talk about some of these now.

51
00:03:02,140 --> 00:03:05,530
So first off, let's look at how we generate and handle errors.

52
00:03:05,530 --> 00:03:09,910
So I'm going to create a function here called Function New‑Error,

53
00:03:09,910 --> 00:03:12,220
and then we're going to use the word Throw.

54
00:03:12,220 --> 00:03:17,640
Now what Throw will do is cause the error to be generated.

55
00:03:17,640 --> 00:03:22,840
So if I say Write‑Error ‑Message "This is an Error" ‑ErrorAction Stop,

56
00:03:22,840 --> 00:03:28,260
this is almost the same as using the New‑Error function using the Throw

57
00:03:28,260 --> 00:03:32,130
word. So Write‑Error will write an error message,

58
00:03:32,130 --> 00:03:35,150
but the ErrorAction is the key to this.

59
00:03:35,150 --> 00:03:38,500
It will force it to stop. So two different approaches.

60
00:03:38,500 --> 00:03:42,540
You can see that I'm throwing an error or I'm throwing an

61
00:03:42,540 --> 00:03:47,140
error and then stopping it from carrying on.

62
00:03:47,140 --> 00:03:47,530
Now,

63
00:03:47,530 --> 00:03:50,970
if we look at how we generate and handle an error, if I go

64
00:03:50,970 --> 00:03:54,340
through this same process again and say New‑Error,

65
00:03:54,340 --> 00:03:57,580
I've got a number, it starts at 0, you can see that I'm

66
00:03:57,580 --> 00:04:01,280
iterating through, and say, okay, error starts at,

67
00:04:01,280 --> 00:04:05,030
you know, it starts at number 1, is it greater than 10, append,

68
00:04:05,030 --> 00:04:08,700
and keep going, you can see I've got Write‑Host, the current

69
00:04:08,700 --> 00:04:10,480
number is whatever the number is,

70
00:04:10,480 --> 00:04:14,640
and then I have an arbitrary line in there that says Throw "This is an

71
00:04:14,640 --> 00:04:20,640
Error". When I execute that, it will throw that error irrelevant of where

72
00:04:20,640 --> 00:04:27,810
that is. Now, if I use that same syntax, that same command and say New‑Error

73
00:04:27,810 --> 00:04:31,990
with an ErrorAction, when the throw occurs,

74
00:04:31,990 --> 00:04:34,910
it will stop the function from executing.

75
00:04:34,910 --> 00:04:38,430
So you just need to be aware that there's different ways of handling it,

76
00:04:38,430 --> 00:04:43,840
and you may need the syntax to continue executing, let you know about the error,

77
00:04:43,840 --> 00:04:48,220
or you may need to throw the error and stop. Now the

78
00:04:48,220 --> 00:04:50,960
ErrorAction parameter has specific values.

79
00:04:50,960 --> 00:04:54,680
The first one is Continue. So this will log the error, display

80
00:04:54,680 --> 00:04:58,340
it to you, and then continue processing.

81
00:04:58,340 --> 00:05:01,630
The next would be to Stop. That will log the error,

82
00:05:01,630 --> 00:05:06,210
display the error, and then terminate. SilentlyContinue

83
00:05:06,210 --> 00:05:08,150
is one that I often find people using.

84
00:05:08,150 --> 00:05:11,270
I would advise not using that one unless you know for sure

85
00:05:11,270 --> 00:05:13,450
that the error isn't anything to worry about.

86
00:05:13,450 --> 00:05:17,440
But this will log an error, doesn't display anything, and just carries,

87
00:05:17,440 --> 00:05:21,770
which can have an impact on the rest of the syntax.

88
00:05:21,770 --> 00:05:23,250
And then, of course, you've got Ignore.

89
00:05:23,250 --> 00:05:25,650
It doesn't log an error, it doesn't display anything,

90
00:05:25,650 --> 00:05:27,840
and it just continues processing.

91
00:05:27,840 --> 00:05:32,710
So choose wisely on those last two, SilentlyContinue and Ignore.

92
00:05:32,710 --> 00:05:36,140
I wouldn't advise you use those all the time.

93
00:05:36,140 --> 00:05:44,000
I would rather you went through, and if you're using the ErrorAction, that you would use Continue and Stop as you need them.

