如何在Powershell中重新捕获catch块的异常? [英] How can I Rethrow an exception from catch block in Powershell?

查看:90
本文介绍了如何在Powershell中重新捕获catch块的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该以与.Net相同的方式处理异常处理?



然后,如何在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆