Now the most important way that people kind of catch these errors
is by using what's referred to as the TRY/CATCH,
TRY/CATCH/FINALLY statements.
Now, of course, there are three of these, so the first one is a TRY/CATCH.
This will try the section of code that you wish to execute,
and if it throws an error,
it will drop it into a catch where you can do something else.
The TRY/FINALLY doesn't handle the error.
It simply executes the code if the exception occurs.
The TRY/CATCH/FINALLY will be the combination of throwing errors and
executing code. So you don't have to use all of them.
You can pick whichever one works for the script that you're
writing. So if I want to generate an exception,
I'm going to use our standard function.
So a standard function of New‑Error Throw "This is an Error."
That will cause me to have an exception.
If I was to wrap this around in a TRY/CATCH,
let's say I was calling that function, I could say try
New‑Error, and if the new error caused an issue,
I could then catch that,
which is the next line down, and then it would write a
value saying An Exception was Generated.
So the try and the catch are very similar syntax to
the loops that we were looking at, where we're,
you know,
saying an if and else or we're looping through something and saying
if this else if else, or the same as a switch statement, which would
be switch conditions and then a default. It's the same logic here. So
we test something in the try.
If it fails, we catch.
If we were to change this to use the FINALLY,
then what you can see is we have try and finally, it will go and
say I'm using a function called New‑Message,
which is going to write a message out, and it will say
New‑Message finally Write‑Output.
So if it continues to error,
it will just capture the error but then drop down
and carry on and execute the code.
Now we could mix this all together and use the TRY/CATCH with the
FINALLY. So you can see I'm calling my test,
which is my New‑Message, which will have my throw code in it.
It's going to throw the message an error.
I'm then going to catch it,
and it will write the value out. Now, depending on the logic here,
it could either go to the catch or it would drop out to the finally.
So depending on how you structure this,
you can either catch it or you can go to the finally and write the message out.
But we'll look at this one in a few minutes.
We also have what's called typed exception handling available to us.
So exceptions that have a specific type, you can specify the type of
exception that you're trying to catch.
So if it's a piece of string value,
then you can say, This is the type of exception I wish you to catch.
You can catch multiple exception types with a single TRY/CATCH statement.
So what does that look like in reality?
So I have a path here, so C:\Documents\Code.
I do a try, and the try says New‑Error ‑Path, ‑ErrorAction Stop.
So that's my initial try.
If I go underneath that,
I can then use the catch statement here, and notice what I'm
doing. Instead of it just catching the error, I'm looking
for specific types of errors.
So I'm looking for DirectoryNotFound or FileNotFound,
and then I'm going to write a value.
If I go to the catch, I could also do just an IOException so it couldn't
open the file. So we can now strongly type the catch.
So instead of it catching every exception, it's only going to catch it if it matches that strongly typed configuration.