Jef*_*art 3 error-handling powershell powershell-trap
我无法理解为什么我在Powershell中看到这种行为:
PS C:\> trap { "Got it!" } 1/0
Attempted to divide by zero.
At line:1 char:22
+ trap { "Got it!" } 1/0 <<<<
PS C:\> trap { "Got it!" } 1/$null
Got it!
Attempted to divide by zero.
At line:1 char:22
+ trap { "Got it!" } 1/$ <<<< null
为什么一个表达式会触发陷阱而另一个表达式不会?
我会认为你的第一个案例是一个解析错误.这就是解析器试图在该点进行常量折叠(预计算值)和错误,因为它得到除零异常.其他语法错误的行为方式相同,即它们不会触发陷阱:
trap { "Got it!" } 1/;
You must provide a value expression on the right-hand side of the '/' operator.
如果您将代码更改为:
$denom = 0
trap { "Got it!" } 1/$denom
Got it!
Attempted to divide by zero.
然后陷阱触发,因为解析器无法再预计算该值.
| 归档时间: | 
 | 
| 查看次数: | 789 次 | 
| 最近记录: |