Examples

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

Try  

{  

Get-ChildItem  

}  

catch  

{  

"Error in a 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:

Try  

{  

Get-Child  

}  

catch  

{  

"Error in a 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.

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *