WEBVTT

00:03.060 --> 00:08.250
Windows PowerShell uses the tri catch finally construct to implement error handling.

00:09.090 --> 00:13.440
Note Windows PowerShell also uses the trap construct for error handling.

00:14.250 --> 00:20.340
This construct was introduced in Windows PowerShell 1.0 and is more difficult and less flexible than

00:20.340 --> 00:21.240
try catch.

00:21.270 --> 00:25.560
Finally, you should use try catch finally in new scripts.

00:26.460 --> 00:30.300
As the name implies, the construct consists of up to three sections.

00:31.200 --> 00:34.080
The try section contains commands that may have an error.

00:35.010 --> 00:40.410
These commands must have their error actions set to stop, or the error action preference variable must

00:40.410 --> 00:41.310
be set to stop.

00:42.180 --> 00:43.590
This section is required.

00:44.460 --> 00:48.570
The catch section is optional and will run if an error happens in the try section.

00:49.410 --> 00:54.630
The final section is optional and will run regardless of whether an error happens in the try section

00:55.470 --> 00:56.340
if it is used.

00:56.370 --> 00:58.080
Finally must come after catch.

00:58.980 --> 01:00.660
You do not have to use both catch.

01:00.660 --> 01:04.320
And finally, however, you must use at least one of them.

01:05.160 --> 01:10.230
You can have multiple catch sections configured so that each section handles a different kind of error.

01:11.130 --> 01:15.420
For more information about how to do this, read the above underscore try underscore.

01:15.420 --> 01:16.200
Catch underscore.

01:16.200 --> 01:18.420
Finally help file in Windows PowerShell.

01:19.290 --> 01:20.400
Here is an example.

01:21.250 --> 01:28.660
Try get WMI object class win32 underscore service computer name dollars name error action.

01:28.660 --> 01:29.080
Stop.

01:29.920 --> 01:30.400
Catch.

01:31.260 --> 01:31.590
Right.

01:31.590 --> 01:33.600
Verbose error connecting to dollar name.

01:34.440 --> 01:37.920
Note The final section of this construct is rarely used.

01:38.820 --> 01:42.720
That is why the construct is known as tri catch instead of tri catch.

01:42.750 --> 01:48.780
Finally, remember that setting error action to stop will turn any error into a terminating error.

01:49.680 --> 01:52.320
That means the command will not continue to process.

01:53.280 --> 01:58.230
For this reason, you will usually write your script so that the command is processing only one thing

01:58.230 --> 01:58.830
at a time.

01:59.610 --> 02:04.140
In this example, that means the name would contain only one computer name at a time.

02:04.410 --> 02:08.190
So if one computer name fails, the command is not skipping anything.
