Skip to content

Commit

Permalink
Improve examples (#11236)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdwheeler committed Jul 5, 2024
1 parent ba86d1f commit ec80bd9
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 70 deletions.
43 changes: 27 additions & 16 deletions reference/5.1/Microsoft.PowerShell.Core/About/about_Trap.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: Describes a keyword that handles a terminating error.
Locale: en-US
ms.date: 01/17/2024
ms.date: 07/05/2024
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_trap?view=powershell-5.1&WT.mc_id=ps-gethelp
schema: 2.0.0
title: about Trap
Expand Down Expand Up @@ -274,20 +274,21 @@ For example, when a terminating error occurs in an `foreach` statement, the

```powershell
trap { 'An error occurred!'}
foreach ($x in 3..0) {
1/$x
'after division'
foreach ($x in 3..-1) {
"1/$x = "
"`t$(1/$x)"
}
'after loop'
```

```Output
0.333333333333333
after division
0.5
after division
1
after division
1/3 =
0.333333333333333
1/2 =
0.5
1/1 =
1
1/0 =
An error occurred!
Attempted to divide by zero.
At line:3 char:4
Expand Down Expand Up @@ -447,24 +448,34 @@ The following sample function uses the `continue` keyword in a `trap`
statement:

```powershell
function continue_example {
function ContinueExample {
trap {
'Error trapped'
continue
}
1/$null
'Function completed.'
foreach ($x in 3..-1) {
"1/$x = "
"`t$(1/$x)"
}
'End of function'
}
continue_example
ContinueExample
```

```Output
1/3 =
0.333333333333333
1/2 =
0.5
1/1 =
1
1/0 =
Error trapped
Function completed.
End of function
```

The function resumes after the error is trapped, and the `Function completed`
The function resumes after the error is trapped, and the `End of function`
statement runs. No error is written to the error stream.

## Notes
Expand Down
47 changes: 29 additions & 18 deletions reference/7.2/Microsoft.PowerShell.Core/About/about_Trap.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: Describes a keyword that handles a terminating error.
Locale: en-US
ms.date: 01/17/2024
ms.date: 07/05/2024
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_trap?view=powershell-7.2&WT.mc_id=ps-gethelp
schema: 2.0.0
title: about Trap
Expand Down Expand Up @@ -261,25 +261,26 @@ For example, when a terminating error occurs in an `foreach` statement, the

```powershell
trap { 'An error occurred!'}
foreach ($x in 3..0) {
1/$x
'after division'
foreach ($x in 3..-1) {
"1/$x = "
"`t$(1/$x)"
}
'after loop'
```

```Output
0.333333333333333
after division
0.5
after division
1
after division
1/3 =
0.333333333333333
1/2 =
0.5
1/1 =
1
1/0 =
An error occurred!
RuntimeException:
Line |
3 | 1/$x
| ~~~~
4 | "`t$(1/$x)"
| ~~~~
| Attempted to divide by zero.
after loop
```
Expand Down Expand Up @@ -425,24 +426,34 @@ The following sample function uses the `continue` keyword in a `trap`
statement:

```powershell
function continue_example {
function ContinueExample {
trap {
'Error trapped'
continue
}
1/$null
'Function completed.'
foreach ($x in 3..-1) {
"1/$x = "
"`t$(1/$x)"
}
'End of function'
}
continue_example
ContinueExample
```

```Output
1/3 =
0.333333333333333
1/2 =
0.5
1/1 =
1
1/0 =
Error trapped
Function completed.
End of function
```

The function resumes after the error is trapped, and the `Function completed`
The function resumes after the error is trapped, and the `End of function`
statement runs. No error is written to the error stream.

## Notes
Expand Down
47 changes: 29 additions & 18 deletions reference/7.4/Microsoft.PowerShell.Core/About/about_Trap.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: Describes a keyword that handles a terminating error.
Locale: en-US
ms.date: 01/17/2024
ms.date: 07/05/2024
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_trap?view=powershell-7.4&WT.mc_id=ps-gethelp
schema: 2.0.0
title: about Trap
Expand Down Expand Up @@ -261,25 +261,26 @@ For example, when a terminating error occurs in an `foreach` statement, the

```powershell
trap { 'An error occurred!'}
foreach ($x in 3..0) {
1/$x
'after division'
foreach ($x in 3..-1) {
"1/$x = "
"`t$(1/$x)"
}
'after loop'
```

```Output
0.333333333333333
after division
0.5
after division
1
after division
1/3 =
0.333333333333333
1/2 =
0.5
1/1 =
1
1/0 =
An error occurred!
RuntimeException:
Line |
3 | 1/$x
| ~~~~
4 | "`t$(1/$x)"
| ~~~~
| Attempted to divide by zero.
after loop
```
Expand Down Expand Up @@ -425,24 +426,34 @@ The following sample function uses the `continue` keyword in a `trap`
statement:

```powershell
function continue_example {
function ContinueExample {
trap {
'Error trapped'
continue
}
1/$null
'Function completed.'
foreach ($x in 3..-1) {
"1/$x = "
"`t$(1/$x)"
}
'End of function'
}
continue_example
ContinueExample
```

```Output
1/3 =
0.333333333333333
1/2 =
0.5
1/1 =
1
1/0 =
Error trapped
Function completed.
End of function
```

The function resumes after the error is trapped, and the `Function completed`
The function resumes after the error is trapped, and the `End of function`
statement runs. No error is written to the error stream.

## Notes
Expand Down
47 changes: 29 additions & 18 deletions reference/7.5/Microsoft.PowerShell.Core/About/about_Trap.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: Describes a keyword that handles a terminating error.
Locale: en-US
ms.date: 01/17/2024
ms.date: 07/05/2024
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_trap?view=powershell-7.5&WT.mc_id=ps-gethelp
schema: 2.0.0
title: about Trap
Expand Down Expand Up @@ -261,25 +261,26 @@ For example, when a terminating error occurs in an `foreach` statement, the

```powershell
trap { 'An error occurred!'}
foreach ($x in 3..0) {
1/$x
'after division'
foreach ($x in 3..-1) {
"1/$x = "
"`t$(1/$x)"
}
'after loop'
```

```Output
0.333333333333333
after division
0.5
after division
1
after division
1/3 =
0.333333333333333
1/2 =
0.5
1/1 =
1
1/0 =
An error occurred!
RuntimeException:
Line |
3 | 1/$x
| ~~~~
4 | "`t$(1/$x)"
| ~~~~
| Attempted to divide by zero.
after loop
```
Expand Down Expand Up @@ -425,24 +426,34 @@ The following sample function uses the `continue` keyword in a `trap`
statement:

```powershell
function continue_example {
function ContinueExample {
trap {
'Error trapped'
continue
}
1/$null
'Function completed.'
foreach ($x in 3..-1) {
"1/$x = "
"`t$(1/$x)"
}
'End of function'
}
continue_example
ContinueExample
```

```Output
1/3 =
0.333333333333333
1/2 =
0.5
1/1 =
1
1/0 =
Error trapped
Function completed.
End of function
```

The function resumes after the error is trapped, and the `Function completed`
The function resumes after the error is trapped, and the `End of function`
statement runs. No error is written to the error stream.

## Notes
Expand Down

0 comments on commit ec80bd9

Please sign in to comment.