Javatpoint Logo
Javatpoint Logo

PowerShell Try Catch Finally

When you need to handle the terminating errors within the script blocks, use a Try, Catch, and finally blocks in a PowerShell.

The terminating errors are those errors which stop the statement from running. When PowerShell does not handle the terminating errors in some way, then it also stops running a script or function using the current pipeline. In other languages, such as C, these errors are referred to as exceptions.

Try {........}

Try block is the part of a script where you want PowerShell to monitor for errors. When an error occurs in this block, it is first stored in the automatic variable $Error. After that, PowerShell searches for the Catch block to handle it.

If the Try block does not have a matching Catch block, then the PowerShell searches in the parent scopes for an appropriate Trap or Catch block.

Syntax of Try Block

The following box shows the syntax of the try block:

A Try statement must have at least one catch or one finally block. In the above syntax, the Try keyword is followed by single or multiple statements in the braces.

Catch {.....}

The Catch block is the part in the script which handles the errors generated by the Try block. We can define which type of error to be handled by the Catch block. A type of error is an exception of a Microsoft .NET framework. A Try block can have multiple catch blocks for different types of errors. Instead of the Catch block, you can also use the Trap block for handling the errors.

Syntax of Catch block

The following box shows the syntax of the catch block:

The types of errors appear in the brackets. The outermost bracket shows that the element is optional. In the above syntax, the catch keyword is followed by an optional list of error type specifications and a list of statements. When anyone catch block is found, then the statements in the catch block are executed.

Finally {......}

The ‘finally’ block is a part of a script which is used to free a resource that no longer needed by a script.

The following box shows the syntax of the Finally block:

The ‘finally’ block is followed by single or multiple statements which are enclosed in the braces that execute every time when the script is executed.

Examples

Example1: The following example uses only one Catch block with the Try block:

In this example, the command is correct in the Try block, so there is no error and displays the following output:

    Directory: C:\
Mode      LastWrite        Time         Length Name
----           -------------       ------            ----
d-----       23-02-2019     13:14                found.000
d-----       28-12-2017     19:44                Intel
d-----       15-09-2018     13:03                PerfLogs
d-----       09-10-2019     11:20                powershell
d-r---       15-11-2019     12:01                Program Files
d-r---       15-11-2019     12:23                Program Files (x86)
d-----       22-08-2019     15:20                Temp
d-----       13-07-2019     09:55                TURBOC3
d-r---       29-09-2019     16:20                Users
d-----       15-11-2019     18:06                Windows
d-----       29-01-2019     18:26                xampp
d-----       05-05-2019     12:53                xampplite
-a----       20-11-2019     04:26        4684056 aow_drv.log  

Example2: The following example also uses only one Catch block with the Try block:

In this example, the command is wrong in the Try block, so there exists an error in a Try block. Hence, we get the output from the catch block:

Error in a Try block.





Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA