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?
Is throw is enough? Or would throw $_ be better?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you would like to re-throw original exception you could use
throw(most common), orthrow $_, orthrow $_.Exceptionps: inside
catchvariable$_is not exception by itself, butSystem.Management.Automation.ErrorRecordthat contains ExceptionNote
The
throwkeyword at PowerShell behaves differently then .NET implementation: in .NET you can only throwSystem.Exceptionsitself or its successors, but in PowerShell, you can throw anything and that is automatically wrapped up into aSystem.Management.Automation.RuntimeException. See snippet here.