Skip to content

add explantion to other versions #4108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,47 @@ inherit from the **SystemException** type.
If you specify an error class and one of its derived classes, place the `Catch`
block for the derived class before the `Catch` block for the general class.

### ACCESSING EXCEPTION INFORMATION

Within a `Catch` block, the current error can be accessed using `$_`, which
is also known as `$PSItem`. The object is of type **ErrorRecord**.

```powershell
try { NonsenseString }
catch {
Write-Host "An error occurred:"
Write-Host $_
}
```

Running this script returns the following result:

```Output
An Error occurred:
The term 'NonsenseString' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
```

There are additional properties that can be accessed, such as **ScriptStackTrace**,
**Exception**, and **ErrorDetails**. For example, if we change the script to the
following:

```powershell
try { NonsenseString }
catch {
Write-Host "An error occurred:"
Write-Host $_.ScriptStackTrace
}
```

The result will be similar to:

```
An Error occurred:
at <ScriptBlock>, <No file>: line 2
```

### FREEING RESOURCES BY USING FINALLY

To free resources used by a script, add a `Finally` block after the `Try` and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,47 @@ inherit from the **SystemException** type.
If you specify an error class and one of its derived classes, place the `Catch`
block for the derived class before the `Catch` block for the general class.

### ACCESSING EXCEPTION INFORMATION

Within a `Catch` block, the current error can be accessed using `$_`, which
is also known as `$PSItem`. The object is of type **ErrorRecord**.

```powershell
try { NonsenseString }
catch {
Write-Host "An error occurred:"
Write-Host $_
}
```

Running this script returns the following result:

```Output
An Error occurred:
The term 'NonsenseString' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
```

There are additional properties that can be accessed, such as **ScriptStackTrace**,
**Exception**, and **ErrorDetails**. For example, if we change the script to the
following:

```powershell
try { NonsenseString }
catch {
Write-Host "An error occurred:"
Write-Host $_.ScriptStackTrace
}
```

The result will be similar to:

```
An Error occurred:
at <ScriptBlock>, <No file>: line 2
```

### FREEING RESOURCES BY USING FINALLY

To free resources used by a script, add a `Finally` block after the `Try` and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,47 @@ inherit from the **SystemException** type.
If you specify an error class and one of its derived classes, place the `Catch`
block for the derived class before the `Catch` block for the general class.

### ACCESSING EXCEPTION INFORMATION

Within a `Catch` block, the current error can be accessed using `$_`, which
is also known as `$PSItem`. The object is of type **ErrorRecord**.

```powershell
try { NonsenseString }
catch {
Write-Host "An error occurred:"
Write-Host $_
}
```

Running this script returns the following result:

```Output
An Error occurred:
The term 'NonsenseString' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
```

There are additional properties that can be accessed, such as **ScriptStackTrace**,
**Exception**, and **ErrorDetails**. For example, if we change the script to the
following:

```powershell
try { NonsenseString }
catch {
Write-Host "An error occurred:"
Write-Host $_.ScriptStackTrace
}
```

The result will be similar to:

```
An Error occurred:
at <ScriptBlock>, <No file>: line 2
```

### FREEING RESOURCES BY USING FINALLY

To free resources used by a script, add a `Finally` block after the `Try` and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,47 @@ inherit from the **SystemException** type.
If you specify an error class and one of its derived classes, place the `Catch`
block for the derived class before the `Catch` block for the general class.

### ACCESSING EXCEPTION INFORMATION

Within a `Catch` block, the current error can be accessed using `$_`, which
is also known as `$PSItem`. The object is of type **ErrorRecord**.

```powershell
try { NonsenseString }
catch {
Write-Host "An error occurred:"
Write-Host $_
}
```

Running this script returns the following result:

```Output
An Error occurred:
The term 'NonsenseString' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
```

There are additional properties that can be accessed, such as **ScriptStackTrace**,
**Exception**, and **ErrorDetails**. For example, if we change the script to the
following:

```powershell
try { NonsenseString }
catch {
Write-Host "An error occurred:"
Write-Host $_.ScriptStackTrace
}
```

The result will be similar to:

```
An Error occurred:
at <ScriptBlock>, <No file>: line 2
```

### FREEING RESOURCES BY USING FINALLY

To free resources used by a script, add a `Finally` block after the `Try` and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ block for the derived class before the `Catch` block for the general class.

### ACCESSING EXCEPTION INFORMATION

Within a `Catch` block, the current error can be ascertained using `$_`, which
is also known as `$PSItem`, and the object is of type **ErrorRecord**.
Within a `Catch` block, the current error can be accessed using `$_`, which
is also known as `$PSItem`. The object is of type **ErrorRecord**.

```powershell
try { NonsenseString }
Expand Down