在 tcl 中找不到包 trycatch

cannot find package trycatch in tcl

我正在编写此代码,但发现错误 "can't find package trycatch"。

package require trycatch
namespace import trycatch
package require tcom
package require html
package require Thread
package require tdom
package require SQL

try {
    global configFileValues
    global File
    set logFolder "D:/Work/Smart Test/Logs"

    set windowTextOrWebUrl [lindex $argv 0]
    puts "WindowText: $windowTextOrWebUrl"
    # regsub -all {\[} "$windowTextOrWebUrl" {\[} windowTextOrWebUrl
    # puts "WindowText: $windowTextOrWebUrl"

    proc CreateSqlConnection {} {
        global odbcHandle
        set odbcHandle [SQL::SQLDriverConnect "Driver={MySQL ODBC 5.2a Driver};server=localhost;database=smarttest;user=root;password=pass"]
        return $odbcHandle
    }
    proc ReleaseSqlConnection {} {
        global odbcHandle
        set disconnectVal [$odbcHandle SQLDisconnect]
        # puts $disconnectVal
    }
    proc LogMaintenance {} {
        global logFolder
        global File
        set Time [clock seconds]
        set startTime [clock format $Time -format "%Y-%m-%d"]
        # puts "$logFolder"
        if {[file isdirectory $logFolder] == 0} {
            file mkdir $logFolder
        }
        set fileName "Logs"
        set extension ".txt"
        set logFile [concat $logFolder/$startTime$extension]
        set File [open $logFile a]
        return $File
    }
    proc GetWebControls {url} {
        global odbcHandle
        set iapp [tcom::ref createobj "InternetExplorer.Application"]
        $iapp Visible 1
        $iapp Navigate $url
        while {[$iapp Busy]} { after 500 }
        after 500
        set doc [$iapp Document]
        set body [$doc body]
        set elements [$body all]

        for {set i 0} {$i < [$elements length]} {incr i} {
            set controlType "NULL"
            set controlId "NULL"
            set controlName "NULL"
            set controlCaption "NULL"
            set controlDefaultValue "NULL"
            set controlType [[$elements item $i] getAttribute type]
            puts "ControlType: $controlType"
            switch $controlType {
                "button" {
                    puts "Button"
                }
                "text" {
                    # puts "Text"
                    set controlId [[$elements item $i] getAttribute id]
                    set controlCaption [[$elements item $i] getAttribute name]
                    set controlDefaultValue [[$elements item $i] getAttribute value]
                    set sqlQuery "Insert into t_controls (cntType, cntIndex, cntName, cntDefaultValue, cntCaption, cntScrId, modifiedBy, modifiedOn,    createdBy,createdOn)
                    Values ('$controlType',NULL,'$controlId','$controlDefaultValue','$controlCaption',1,'BalGovind',NOW(),'BalGovind',NOW())"
                    # puts $sqlQuery
                    set queryResult [$odbcHandle SQLExecDirect $sqlQuery]
                    puts "Query result in WebControl(): $queryResult"
                }
                "submit" {
                    # puts "submit"
                    set controlId [[$elements item $i] getAttribute id]
                    set controlName [[$elements item $i] getAttribute name]
                    set controlCaption [[$elements item $i] getAttribute value]
                    set sqlQuery "Insert into t_controls (cntType, cntIndex, cntName, cntDefaultValue, cntCaption, cntScrId, modifiedBy, modifiedOn,    createdBy,createdOn)
                    Values ('$controlType',NULL,'$controlId',NULL,'$controlCaption',1,'BalGovind',NOW(),'BalGovind',NOW())"
                    # puts $sqlQuery
                    set queryResult [$odbcHandle SQLExecDirect $sqlQuery]
                    puts "Query result in WebControl(): $queryResult"
                }
                "password" {
                    # puts "password"
                    set controlId [[$elements item $i] getAttribute id]
                    set controlCaption [[$elements item $i] getAttribute name]
                    set controlDefaultValue [[$elements item $i] getAttribute value]
                    set sqlQuery "Insert into t_controls (cntType, cntIndex, cntName, cntDefaultValue, cntCaption, cntScrId, modifiedBy, modifiedOn,    createdBy,createdOn)
                    Values ('$controlType',NULL,'$controlId','$controlDefaultValue','$controlCaption',1,'BalGovind',NOW(),'BalGovind',NOW())"
                    # puts $sqlQuery
                    set queryResult [$odbcHandle SQLExecDirect $sqlQuery]
                    puts "Query result in WebControl(): $queryResult"
                }
                "label" {
                    # puts "label"
                    set controlId [[$elements item $i] getAttribute id]
                    set controlCaption [[$elements item $i] getAttribute value]
                    set sqlQuery "Insert into t_controls (cntType, cntIndex, cntName, cntDefaultValue, cntCaption, cntScrId, modifiedBy, modifiedOn,    createdBy,createdOn)
                    Values ('$controlType',NULL,'$controlId','$controlDefaultValue','$controlCaption',1,'BalGovind',NOW(),'BalGovind',NOW())"
                    # puts $sqlQuery
                    set queryResult [$odbcHandle SQLExecDirect $sqlQuery]
                    puts "Query result in WebControl(): $queryResult"
                }
            }
            unset controlType
            unset controlId
            unset controlName
            unset controlCaption
            unset controlDefaultValue
        }
    }
    proc GetWindowControls {windowText} {
        set app [::tcom::ref createobj "ManagedSpy.Utility.Utility"]
        set bool [$app FetchControls $windowText]
        if {$bool == 0} {               
            exit
        }
    }
    proc GetControls {screenName} {
        global windowTextOrWebUrl
        set screenName $windowTextOrWebUrl
        if {([regexp "www." "$screenName" matchedString]) || ([regexp "http" "$screenName" matchedString])} {
            puts "Web Application."
            GetWebControls $screenName
        } else {
            puts "Window Application."
            GetWindowControls $screenName
        }
    }
    proc Main {} {
        puts "Executing Main Procedure!!!"
        global File
        global odbcHandle
        global windowTextOrWebUrl
        set File [LogMaintenance]
        set currentTime [clock format [clock seconds] -format "%H-%M-%S-%p"]
        puts $File "$currentTime :: Script started."
        set odbcHandle [CreateSqlConnection]
        GetControls $windowTextOrWebUrl
        ReleaseSqlConnection
        puts $File "[clock format [clock seconds] -format "%H-%M-%S-%p"] :: Script ends.\n\n######################################### \n"
        close $File
    } 
} 
catch -msg msg -code code -info info {
    puts "catch"
    puts "$msg $info"
    puts $File "[clock format [clock seconds] -format "%H-%M-%S-%p"] :: Exception - $msg \n $code \n $info \n"
    puts $File "[clock format [clock seconds] -format "%H-%M-%S-%p"] :: Script is ended.\n\n################################################## \n"
    close $File
}

trycatch 包的文档是 online, and I'd guess that the sources are here. However, you probably ought to consider switching to Tcl 8.6 which has built-in equivalent functionality

您的代码似乎还有其他问题。很少需要在错误捕获上下文中声明过程;将 calls 放在这些过程中更为常见……