2016-03-03 90 views
0

我正在編寫代碼,我發現錯誤「找不到包trycatch」。在tcl中找不到包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 
} 

回答

1

trycatch包的文檔online,和我猜的來源是here。但是,您可能應該考慮切換到已內置equivalent functionality的Tcl 8.6。

您的代碼似乎也有其他問題。程序很少需要在錯誤捕捉的上下文中聲明;通常把調用內部的那些程序...

+0

但現在我在第二行是「名稱空間導入trycatch」的錯誤。錯誤是沒有在導入模式中指定名稱空間trycatch –

+0

擺脫'package require trycatch'和'namespace import'行(這是buggy)。 –