繁体   English   中英

如何从 catch 块分支到 finally - Powershell

[英]How to branch from the catch block to finally - Powershell

我知道代码的“最终”部分将始终运行,但我没有看到它发生在我的代码中。 请帮忙。

这是我的一段代码:

$ErrorActionPreference = "Stop"

$connection.ConnectionString = "Data Source = " + $TDEnv + ";Connection Pooling Timeout=300;User Id=" + $TDUserID + ";Password=" + $TDPswd + ";"
try{
    $connection.Open()
}
catch{
    Write-Host "An error occurred acquiring the connection to Teradata"
    Write-Host $_.Exception.Message
    Exit 99
}

$SqlCommand = "SELECT TableName AS TDProcName FROM dbc.TablesV WHERE 1 = 1 AND TableKind = '" + $TableKind + "' AND DatabaseName = '" + $DBName + "';"
$command = $connection.CreateCommand()
$command.CommandText = $sqlCommand

$adapter = $factory.CreateDataAdapter()
$adapter.SelectCommand = $command

$dataset = new-object System.Data.DataSet


try{
    [void] $adapter.Fill($dataset)

    $ProcNames = $dataset.Tables | Select-Object -Expand Rows

    #------Build the actual BTeq script------#
    ForEach($procNm in $ProcNames){

        #--------------------- This needs to be changed to make it dynamic, based on the param passed in ---------------------#
        $bTeqScrObj = $bTeqScrObj + ".EXPORT DATA FILE = E:\TFSObjects\BI\" + $Environment + "\Teradata\Procs\" + $procNm.Item(0).ToString() + ".sql; `r`n"
        $bTeqScrObj = $bTeqScrObj + ".SET RECORDMODE OFF; `r`n"
        $bTeqScrObj = $bTeqScrObj + "SHOW " + $ObjName +  "  " + $DBName + "." + $procNm.Item(0).ToString() + "; `r`n `r`n `r`n"
        #$bTeqScrObj = $bTeqScrObj + ".EXPORT RESET; `r`n `r`n `r`n"
    }

    $bTeqScrObj = $bTeqScrObj + ".LOGOFF; `r`n `r`n `r`n"
    $bTeqScrObj = $bTeqScrObj + ".EXIT 0;"

    Write-Host "This shouldnt show up on the screen `r`n"

    #----Materialize the script object to a file----#
    $bTeqScrObj | Out-File -FilePath $OutFile

    #------Run the actual BTeq script------#
    cat $OutFile | bteq
}
catch{
    Write-Host "An error occurred while processing the request:"
    Write-Host $_       
}   

finally{

        Write-Host "Yep, still got here!"

        if($connection.State -eq "Open"){
            $connection.Close()
        }

    }

但是我看到的是代码的最后部分没有显示出来。 所以,我希望代码从第一个 catch 块分支到 finally。 为什么没有发生?

但是,如果第二个 try/catch 块中出现错误,它会转到 finally 部分。

PS:我尝试删除Exit,将其替换为throw,结果仍然相同。

$ErrorActionPreference = "Stop"

$connection.ConnectionString = "Data Source = " + $TDEnv + ";Connection Pooling Timeout=300;User Id=" + $TDUserID + ";Password=" + $TDPswd + ";"
try{
    $connection.Open()
}
catch{
    Write-Host "An error occurred acquiring the connection to Teradata"
    Write-Host $_.Exception.Message
    Exit 99
}
finally {
    CleanUpDbConnection($connection)
}

$SqlCommand = "SELECT TableName AS TDProcName FROM dbc.TablesV WHERE 1 = 1 AND TableKind = '" + $TableKind + "' AND DatabaseName = '" + $DBName + "';"
$command = $connection.CreateCommand()
$command.CommandText = $sqlCommand

$adapter = $factory.CreateDataAdapter()
$adapter.SelectCommand = $command

$dataset = new-object System.Data.DataSet


try{
    [void] $adapter.Fill($dataset)

    $ProcNames = $dataset.Tables | Select-Object -Expand Rows

    #------Build the actual BTeq script------#
    ForEach($procNm in $ProcNames){

        #--------------------- This needs to be changed to make it dynamic, based on the param passed in ---------------------#
        $bTeqScrObj = $bTeqScrObj + ".EXPORT DATA FILE = E:\TFSObjects\BI\" + $Environment + "\Teradata\Procs\" + $procNm.Item(0).ToString() + ".sql; `r`n"
        $bTeqScrObj = $bTeqScrObj + ".SET RECORDMODE OFF; `r`n"
        $bTeqScrObj = $bTeqScrObj + "SHOW " + $ObjName +  "  " + $DBName + "." + $procNm.Item(0).ToString() + "; `r`n `r`n `r`n"
        #$bTeqScrObj = $bTeqScrObj + ".EXPORT RESET; `r`n `r`n `r`n"
    }

    $bTeqScrObj = $bTeqScrObj + ".LOGOFF; `r`n `r`n `r`n"
    $bTeqScrObj = $bTeqScrObj + ".EXIT 0;"

    Write-Host "This shouldnt show up on the screen `r`n"

    #----Materialize the script object to a file----#
    $bTeqScrObj | Out-File -FilePath $OutFile

    #------Run the actual BTeq script------#
    cat $OutFile | bteq
}
catch{
    Write-Host "An error occurred while processing the request:"
    Write-Host $_       
}   

finally{

        Write-Host "Yep, still got here!"

        CleanDbConnection($connection)

    }`enter code here`


function CleanDbConnection {
      if($args[0].State -eq "Open"){
                $args[0].Close()
            }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM