The chain operators don't work with flow-control statements exit, return, and Throw
See original GitHub issueA common idiom (in the Bash world, which inspired PowerShell’s &&
and ||
operators) is to conditionally exit a script when invocation of a command fails, along the lines of:
# Assume existence of /somepath and exit, if it doesn't exist.
ls /somepath || exit 1
Currently, neither exit
nor return
nor throw
can be used on the RHS of &&
/ ||
Steps to reproduce
{ Get-Item /nosuch || return } | Should -Not -Throw
{ Get-Item / && return } | Should -Not -Throw
Expected behavior
The tests should pass.
Actual behavior
The tests fail, because return
is not recognized
Expected no exception to be thrown, but an exception
"The term 'return' is not recognized as the name of a
cmdlet, function, script file, or operable program. ..."...
Environment data
PowerShell Core 7.0.0-preview.6
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:33 (14 by maintainers)
Top Results From Across the Web
Control flow and error handling - JavaScript - MDN Web Docs
throw statement. Use the throw statement to throw an exception. A throw statement specifies the value to be thrown: js
Read more >Are exceptions as control flow considered a serious ...
Exceptional situations are handled by the raise / rescue operators. But you can use throw / catch for exception-like control flow constructs**.
Read more >c++ - How to avoid "if" chains?
The problem with the guard pattern is that it relies on what is called "opportunistic return" or "opportunistic exit." In other words, it ......
Read more >Control Flow - Julia Documentation
Julia provides a variety of control flow constructs: ... This value is simply the return value of the last executed statement in the...
Read more >Control Flow - Documentation - Swift.org
Structure code with branches, loops, and early exits. ... Swift provides a variety of control flow statements. ... Use the half-open range operator...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I wonder how the fact “syntactically it doesn’t work because the big difference between PowerShell and bash” can we made clear to users. I had assumed that this new feature would basically work the same in PowerShell as in bash, hence many others will very likely start with the same assumption. At least this conclusion seems ‘another nail in the coffin’ for the goal ‘pwsh is a replacement to bash’ 😦
@rjmholt
Thank you for putting up a good fight and for laying out the rationale.
Before I even try to understand the intricacies of the grammar, let me make a plea purely from a user’s perspective:
If we force the use of
$(...)
:We burden what is probably the primary use case for
&&
and||
.New users will not expect the need for
$(...)
and won’t necessarily know that it is what is required whenfoo || exit 1
fails (though documentation will help there).Even once users do know about the need for
$(...)
:$(...)
is hard to type.As an aside: The same arguments apply to the unfortunate decision to require
{...}
around identifiers in order to be able to use the null-conditional member-access operator,?.
.So, for the sake of the end-users’ experience, can we look for a solution that works as expected, is easy to explain, and technically not too tortu[r]ous?
Personally, I’m not worried about cases such as
Get-Item ./doesntexist || throw "More things" && "other stuff" &
, because I wouldn’t expect many people to attempt such acrobatics.