如何在Powershell中重新捕获catch块的异常? [英] How can I Rethrow an exception from catch block in Powershell?
问题描述
然后,如何在Powershell中从catch块重新抛出异常? p>
'throw'是enoungh或'throw $ _'会更好吗?
在PowerShell中的
throw
关键字的行为与.NET实现不同:在.net中,您只能抛出 System.Exceptions
本身或其后继者,但在PowerShell中,您可以抛出任何东西,并自动包装成一个 System.Management.Automation.RuntimeException
。请参阅此处的片段。
如果您喜欢重新抛出原始异常,您可以使用 throw
(最常见),或 throw $ _
或 throw $ _。例外
ps:inside catch
code> $ _ 本身不是例外,但 System.Management.Automation.ErrorRecord
包含异常
Should I approach the exception handling in the same manner as .Net?
Then, how can I re-throw an exception from catch block in Powershell?
'throw' is enoungh or 'throw $_' would be better?
throw
keyword at PowerShell behaves differently then .net implementation: in .net you can only throw System.Exceptions
itself or its successors, but in PowerShell, you can throw anything and that is automatically wrapped up into a System.Management.Automation.RuntimeException
. See snippet here.
If you like to re-throw original exception you could use throw
(most common), or throw $_
, or throw $_.Exception
ps: inside catch
variable $_
is not exception by itself, but System.Management.Automation.ErrorRecord
that contains Exception
这篇关于如何在Powershell中重新捕获catch块的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!