Non-Terminating Errors 通过Write-Error 触发的错误称为 Non-Terminating Errors。本质上它只是把错误信息写入了输出流中而没有产生异常,所以默认情况下 catch 语句无法捕获 Non-Terminating Errors。默认情况下,Non-Terminating Errors 不影响后面命令的执行! 把下面的代码保存到文
Treating Non-Terminating Errors as Terminating So how do you catch a Non-Terminating error? Basically, you tell PowerShell to treat it as terminating. To do this you use the ErrorAction parameter. Every PowerShell cmdlet supports ErrorAction. By specifying -ErrorAction Stop on the end of a c...
}else{// If a name parameter is passed to cmdlet, get and write// the associated processes.// Write a non-terminating error for failure to retrieve// a process.foreach(stringnameinprocessNames) { Process[] processes;try{ processes = Process.GetProcessesByName(name); } catc...
由于$PSItem 是ErrorRecord,因此还可以使用 ThrowTerminatingError 以这种方式重新引发。PowerShell 复制 catch { $PSCmdlet.ThrowTerminatingError($PSItem) } 这会将错误源更改为 Cmdlet,并向 Cmdlet 用户隐藏函数内部信息。Try 可能会创建终止错误Kirk Munro 指出,某些异常仅在 try/catch 块内执行时为终止错误。
建议使用 System.Management.Automation.Cmdlet.ThrowTerminatingError* 而不是引发异常,因为错误记录提供了有关错误条件的其他信息,这对最终用户很有用。 Cmdlet 应遵循托管代码准则,防止捕获和处理所有异常(catch (Exception e))。 仅将已知和预期类型的异常转换为错误记录。 另请参阅 System.Management.Automation....
Hey, Scripting Guy! Yesterday inError Handling: Two Types of Errors, you were talking about terminating errors and how you can useTry/Catch/Finallywith those, but you did not really say what a non-terminating error is. Can you talk about that for a little bit please?
Catch error from Invoke-RestMethod catch return value from script in batch file Catching errors and outputting to log file change a cell value in excel using powershell Change Baud Rate or Bits Per Second COM Port X with Powershell Change Cell Color in HTML Table when match a value Change ...
问带有Git命令错误处理的Powershell -在外部程序的非零退出代码时自动中止ENcentos 启动一个容器添加了-d...
Java的基本理念是“结构不佳的代码不能运行”,在我们进行编写代码的时候一般通过编译的时候就可以看出...
A possible solution is to make the behavior similar to that of .NET method calls (whose exceptions are treated as non-terminating errorswithouttry/catch, butarecaught if enclosed intry/catch- although that could be considered a historical wart itself); translated to this scenario: ...