第三方实用程序的异常处理不起作用

问题描述:

我想在powershell中使用cleartool命令。第三方实用程序的异常处理不起作用

如果命令失败,它应该捕获异常并执行操作。但它并没有被catch {}捕获{}

try { 
     #If $viewname not exist it will throw error 
     cleartool lsview $ViewName 
    } 
catch { 

    # If list view fails , it means View doesn't exist. So create view 
    Write-host "Create view" 
    cleartool mkview -tag $ViewName -nsh $ccViews$ViewName".vws" 

} 

当try中的命令失败时,它不会在catch中调用表达式。

是否catch命令不能与非.net相关的东西工作?

我从来没有见过cleartool的PowerShell脚本中使用的异常机制。
(我看到的这对夫妇在“how to find root[folder] for each component using cleartool?”和“How to describe recommend baseline with pipeline”)。

old thread (2006, so for the first version of Powershell)说明使用$?的错误管理机制:

cleartool lsco -cview -s . | 
foreach { 
    cleartool diff -pred -opt -sta "$_" 
    if ($?) { 
    cleartool unco -rm "$_" 
    } else { 
    cleartool ci -nc "$_" 
    } 
} 

要使用的机制,你可能要封装你cleartool呼叫的调用命令,并从包装函数返回一个状态码,如“catching return code of a command with “invoke-command” - Powershell 2”中所述。

或者,而不是调用直接cleartool,你可以尝试调用CAL commands as in this script

+0

例子:http://cmdosndonts.blogspot.com/2010/11/enhancing-clearcase-with-powershell.html,但没有抓住。 CAL API:http://*.com/a/8550920/6309 – VonC 2012-02-16 09:07:49

由于cleartool是一个外部exe文件,它不会在PowerShell环境中引发异常。

根据IBM documentation:如果通过在交互模式下输入quit命令退出cleartool,则退出状态为0.单命令模式下的退出状态取决于命令是成功(零退出状态)还是生成错误消息(非零退出状态)。

在PowerShell中,您可以通过$LASTEXITCODE var获得此非零退出状态。正如@VonC所解释的那样,您可以使用$?来检查单指令是否有效,然后用$LASTEXITCODE来得到特定的错误。 PowerShell脚本的