So let's go into the PowerShell console.
We'll look at generating some errors and exceptions,
and then we'll look at how to handle those errors and exceptions.
One of the downsides of using any kind of programming construct is,
of course, getting errors in your script that you're running.
So maybe you execute a common command, and it fails because,
for example, like we did previously,
where it was the wrong input value or the wrong type,
and then an error is thrown, which kind of messes up the code.
And so one of the most important things about building PowerShell scripts
is to be able to handle the errors in a graceful way.
Now, one of the options that we have is obviously to do what's called Throw,
which is to throw a specific error.
So what we'll do is we'll just create a kind of
basic function within PowerShell.
So let me just do function, and we'll call it New‑Error.
We'll obviously do kind of brackets here.
Let's do this.
And we'll use the word Throw, and then I'll do a message, "This is an error".
So let's do that.
And then we'll close that like so.
Okay, so now we have a function that's available to us,
so I can say New‑Error, and then instantly when we run it,
it throws that error.
And you can see in PowerShell 7 it highlights that for you.
So it'll come back with an exception error,
and then it will say, this is the line or the object that through that error.
So when we're trying to handle things, you could just do this.
You could say, Throw an Error and literally get rid of it.
Now, of course,
that doesn't really help too much when you're trying to build
something that's a little bit easier to handle and can handle the
user flow and the user interaction.
So another option that we've got is to actually use
something called Write‑Error, which allows me to write an error out.
So if I just do this, Write‑Error,
and then it's got a property which is available to us called ‑Message,
so I'm going to add a message, "This is an error",
so the same message.
And then what we get is this kind of error action that's available to us.
Now before we do that, let me just press Enter here.
Now, this will literally just right the error message and say,
"This is an error".
Nothing spectacular.
But if we're trying to handle that process,
then we can also use what's called ‑ErrorAction ID.
And if I just do this one, we can tab, SilentlyContinue, Stop, Suspend.
So we can flick between what we would like to do when an error is generated.
So if I say Write‑Error ‑Message and Stop,
now, obviously, it's just one line, so it's just going to stop.
This will cause the PowerShell function,
method, whatever it is to actually stop working at that point.
So what does that look like if we tie everything together?
So let's go back and do our New function again, so New‑Error.
We'll repeat the same process as before,
but this time we'll do a little bit of a calculation,
so kind of using some of the things that we've done previously.
So for i, so let's do $i is a variable = 1.
So we're using a for loop here,
and then we'll say as long as the i is less than or equal to 10,
so we're going to do a bit of a count here.
I can then do i and ++.
Okay, so let's do that first bit.
What I'm going to do is write a message to the screen,
so what we've done in the past.
"The current number is, and then, of course,
we've got this variable of i that's being populated,
that we can then render inside this message here,
so we'll do this.
And then we'll go down to the next line,
a nd then what we'll do is we'll throw an error message at this point.
So we'll say, "This is an Error", like so,
go underneath it,
and then we actually want to increment a number tw whatever that would be.
So add that one again, close it out, and then we can close the function out.
So we now have our new function.
So let's just review.
We have a New‑Error function.
It starts with the number 0,
and then it counts from basically 1 to everything that's
less than 10 by adding a number to it,
and then what it will do is it will continue to add and write a message,
and then at some point it's going to throw an error.
So what we'll do is we actually run it straightaway,
and, of course, what happens?
It just says the current number is 1, and then because it threw the error,
it cannot do anything else.
So throwing an error will literally cause everything to stop.
So let's go a little step further,
and we'll take that same function that we just built.
So let me just loop through my commands a little bit here.
So there's my clear command, New‑Error.
Not the Write‑Host.
Let's go number, function.
There we go.
Function number is 0.
We'll go back to the for loop.
We'll then do the same command as before for the Write‑Host.
Now this time we're not going to throw an error.
We're going to say Write‑Error instead, so exactly the same thing.
We'll do the message that we're going to enter,
so we'll say, Write‑Error ‑Message.
And so we want to represent the fact that an error was caused.
And I'll say "Error", just like so, to keep it short and sweet.
But then what I'm going to do is do an ‑ErrorAction.
Oops, ‑ErrorAction, like so, and I'm going to say SilentlyContinue.
Do Enter.
And then, of course, I need to add my next line,
which is, if I just go back here, function.
I'll tell you what.
We'll just write that one out, which will be $number += $i,
and then we'll close and close.
So, now I have my function updated.
So before we call it, let's just double check.
Same thing again.
Start with a 0, iterate from 1 to 10, checking that it's less than 10.
It will write a message saying the current number is something.
Then it will write an error, but this time it should silently continue.
So let's just press Enter.
And sure enough, it does.
Now notice it didn't render anything.
It just silently continued and finished all the way to number 10.
So we captured the error.
We effectively grabbed it, and we could have done something.
I chose to write an error, but of course, I told it to silently continue also.
So if we go back and if we go looping through it pretty quickly here,
function, number, for loop, Write‑Host, and change it from SilentlyContinue to,
let's do Suspend, and then let me get rid of this here.
I have to obviously do my $number += $i.
And then we'll close that one down,
and then we'll close the next line down as well.
And so that should then give us our new function, ready to call again.
And this time around we're telling it to cause a suspension of the code,
so I'm going to say Error.
Now, of course, note just what happened.
If you come all the way through here, we can keep scrolling.
It's moaning about a specific action.
You can see that it's looping, and it's trying to do something.
If we look at the message, it says,
The Suspend ActionPreference is reserved for future use.
Perfect.
It's always good to know that when you're trying to use values,
you need to make sure that just because something is documented,
you need to make sure that it's actually useful.
So the effective options here would be to stop or to
actually just silently continue.
So if we do that one more time, number, for loop here,
I'm going to say Stop, and then I'll do my $number += $i,
that one, that one, and that one, and then do New‑Error,
you can see it will just get to number 1 and error.
So it's a simple way of capturing it, a way of trying to handle it,
just rendering a message or breaking or stopping.
It's not the most ideal way of doing it,
but it does allow you to kind of at least when you break and there's an error,
then, of course, you can capture what that error would be.
And, of course, whenever you throw the error,
you can also capture more information about what that message would be.
So if I say Throw "Error", like so, it will say Exception.
But if I say $, let's create a variable here and call it Throw "Error",
like so, now if I do the error,
you can see it gives me a whole host of information that's associated to
that and everything around what the error would be.
So I can say $error., and if I tab through it,
you can see I get the regular properties that are available.
But of course, depending on the name of the variable,
I can also get access to all of the other values too.
Now, I will give you a hint here.
Try not to use kind of the same name as maybe some
of the default values would be, you know,
especially when you're using things like the word error or something else.
They are often reserved words, such as PSItem and things like that.
So you can see I'm just scrolling all the way back through, and
you'll see it's a whole host of different errors and messages that
get rendered from various different things.
So you have the ability to not only throw an error, but to obviously capture the error details.