Skip to content

Commit 836cd56

Browse files
committed
incorporated feedback
1 parent e5b476d commit 836cd56

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

1-Draft/RFCNNNN-Make-Terminating-Errors-Terminate-The-Right-Way.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ By default in PowerShell, terminating errors do not actually terminate. For exam
1515

1616
```PowerShell
1717
& {
18-
1/0
18+
$string = 'Hello'
19+
$string.Substring(99)
1920
'Why?'
2021
}
2122
```
@@ -24,16 +25,32 @@ PowerShell has upheld this behaviour since version 1.0 of the language. You can
2425

2526
```PowerShell
2627
try {
27-
1/0
28+
$string = 'Hello'
29+
$string.Substring(99)
2830
'Why?'
2931
} catch {
3032
throw
3133
}
3234
```
3335

34-
In that example, the exception raised by dividing by zero properly terminates execution of the running command.
36+
You can also convert the terminating-yet-handled-as-a-non-terminating error into an actual terminating error like this:
3537

36-
The difference between these two examples poses a risk to scripters who share scripts or modules with the community. The risk is that end users using a shared resource such as a script or module may see different behaviour from the logic within that module depending on whether or not they were inside of a `try` block when they invoked the script or a command exported by the module. That risk is very undesirable, and as a result many community members who share scripts/modules with the community wrap their logic in a `try/catch{throw}` (or similar) scaffolding to ensure that the behavior of their code is consistent no matter where or how it was invoked.
38+
```PowerShell
39+
& {
40+
$ErrorActionPreference = 'Stop'
41+
$string = 'Hello'
42+
$string.Substring(99)
43+
'Why?'
44+
}
45+
```
46+
47+
In those last two examples, the exception raised by the .NET method terminates execution of the running command.
48+
49+
The difference between the first example and the workarounds poses a risk to scripters who share scripts or modules with the community.
50+
51+
In the first workaround, the risk is that end users using a shared resource such as a script or module may see different behaviour from the logic within that module depending on whether or not they were inside of a `try` block when they invoked the script or a command exported by the module. That risk is very undesirable, and as a result many community members who share scripts/modules with the community wrap their logic in a `try/catch{throw}` (or similar) scaffolding to ensure that the behavior of their code is consistent no matter where or how it was invoked.
52+
53+
In the second workaround, if the shared script does not use `$ErrorActionPreference = 'Stop'`, a caller can get different behaviour by manipulating their `$ErrorActionPreference`. The caller should not be able to manipulate terminating error behavior in commands that they invoke -- that's up to the command author, and they shouldn't have to use extra handling to make terminating errors terminate.
3754

3855
Now consider this code snippet:
3956

0 commit comments

Comments
 (0)