How can I Rethrow an exception from catch block in Powershell?

廉价感情. 提交于 2019-12-18 12:43:34

问题


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?


回答1:


If you would 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

Note

The 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.



来源:https://stackoverflow.com/questions/13820140/how-can-i-rethrow-an-exception-from-catch-block-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!