在PowerShell2.0中允许使用try…catch语句来进行异常处理,那么它与Trap有什么不同呢?下面简要列出:
- Trap:
- Designed for admins
- V1 and V2
- Introduces a new scope
- Is “global”, meaning it applies to all code in the same scope, before or after.
- Does not support rethrow (an empty throw statement throws a special RuntimeException with the message “ScriptHalted”)
- Try/Catch
- Designed for developers
- V2 only
- Does not introduce a new scope
- Guarded code is in the try statement block, not the entire scope containing the try statement
- Supports finally
- Supports rethrowing exceptions
本文参考资料来源:http://blogs.msdn.com/b/powershell/archive/2009/06/17/traps-vs-try-catch.aspx