问题
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