So let's first understand errors and exceptions.
So let's understand the basic terminology. So the first one is an exception.
This is like an event.
It gets created when a normal error handling can't deal with the issue.
The second one is throw and catch. So this happens when an
exception occurs, and so you say that an exception is
thrown. To handle a thrown exception,
you have to catch that exception to do something with it.
Now, along with that is the call stack.
The call stack is the list of functions that were called by each
other that led up to and after the exception.
We then have what's called terminating and non‑terminating errors.
This is an exception that's generally a terminating error.
A thrown exception can either be caught or it terminates the current execution.
And then we have what's called swallowing an exception.
So this is when you catch an error and it just literally disappears.
So it doesn't really serve any purpose,
but you're just capturing it and ignoring it.
So what are the differences between errors and exceptions?
Well, an error is returned as a PowerShell object,
and so it provides the terminating and also non‑terminating errors,
whereas exceptions are created when the normal error
handling cannot handle the issue.
Exceptions are typically non‑terminating errors, effectively.
So what's the difference between a terminating and a non‑terminating? Well,
a terminating one, the error is generated by the script,
function, or commands.
It will officially stop or halt the execution going forward. Whereas a
non‑terminating one, this tends to be generated by internal commands.
It's normally automatically handled so the error doesn't
terminate the execution of the pipeline.
So what are the error handling approaches that we can use?
Well, the first one is to throw the exception.
So as soon as the exception comes,
we capture it, and we just throw the exception,
which effectively will potentially break and stop the execution.
We can do what's called Write‑Error,
so we effectively write a message out and say we found
an error and write the message out.
We could also use what's called ‑ErrorAction,
which is an operator that will then determine what we can do.
There's some predetermined values for the error action.
We could also Try/Catch, the idea being that as it
executes it will try, and if it errors, then we automatically catch that error.
We could then also do what's called Try and Finally,
which is a combination of the Try and the Catch with a default
of Finally action that comes out of there.
And then, of course, we can do Try/Catch/Finally,
So it's really up to you the different options that we've
got, but we'll talk about some of these now.
So first off, let's look at how we generate and handle errors.
So I'm going to create a function here called Function New‑Error,
and then we're going to use the word Throw.
Now what Throw will do is cause the error to be generated.
So if I say Write‑Error ‑Message "This is an Error" ‑ErrorAction Stop,
this is almost the same as using the New‑Error function using the Throw
word. So Write‑Error will write an error message,
but the ErrorAction is the key to this.
It will force it to stop. So two different approaches.
You can see that I'm throwing an error or I'm throwing an
error and then stopping it from carrying on.
Now,
if we look at how we generate and handle an error, if I go
through this same process again and say New‑Error,
I've got a number, it starts at 0, you can see that I'm
iterating through, and say, okay, error starts at,
you know, it starts at number 1, is it greater than 10, append,
and keep going, you can see I've got Write‑Host, the current
number is whatever the number is,
and then I have an arbitrary line in there that says Throw "This is an
Error". When I execute that, it will throw that error irrelevant of where
that is. Now, if I use that same syntax, that same command and say New‑Error
with an ErrorAction, when the throw occurs,
it will stop the function from executing.
So you just need to be aware that there's different ways of handling it,
and you may need the syntax to continue executing, let you know about the error,
or you may need to throw the error and stop. Now the
ErrorAction parameter has specific values.
The first one is Continue. So this will log the error, display
it to you, and then continue processing.
The next would be to Stop. That will log the error,
display the error, and then terminate. SilentlyContinue
is one that I often find people using.
I would advise not using that one unless you know for sure
that the error isn't anything to worry about.
But this will log an error, doesn't display anything, and just carries,
which can have an impact on the rest of the syntax.
And then, of course, you've got Ignore.
It doesn't log an error, it doesn't display anything,
and it just continues processing.
So choose wisely on those last two, SilentlyContinue and Ignore.
I wouldn't advise you use those all the time.
I would rather you went through, and if you're using the ErrorAction, that you would use Continue and Stop as you need them.