【问题标题】:Is it possible to capture the error output of glob?是否可以捕获 glob 的错误输出?
【发布时间】:2020-05-09 23:05:56
【问题描述】:

假设我在try/trap/finally 中包含以下glob 命令:

proc generateSubmissionFolder {cover_letter_resume submission_path} {
   set submission_parent [file dirname $submission_path]
   set latest_submission_folder [lindex [lsort [glob -directory $submission_parent -type d *]] end]
   set latest_submission_file [lindex [glob -directory $latest_submission_folder *[file extension $cover_letter_resume]] end]
   createSubmissionFolder $latest_submission_file $submission_path
}


proc createSubmissionFolder {source destination} {
    puts "Creating $destination folder."
    file mkdir $destination
    puts "Copying $source to $destination"
    file copy $source $destination
}


try {

    # I gathered user input and stored them in the variables $company_name and $position.

    set submission_path [file join $company_name $position $yearmonthday]

    if {[file exists [file dirname $submission_path]]} {
        generateSubmissionFolder $coverletterresume $submission_path
    } else {
        createSubmissionFolder $coverletterresume $submission_path
    } 

} trap {Value Empty} {errormessage} {
   puts "$errormessage"
} finally {
   puts "$argv0 exiting."
}

如果没有找到文件夹,我想提供一个人类可读的错误消息,但我不确定要捕获什么错误。根据answer我之前的问题:

Tcl 没有预定义的异常层次结构。

我尝试的唯一解决方法是使用-nocomplain 开关,然后检查latest_submission_folder 是否为空白。

有没有办法捕获FileNotFoundFolderNotFound 错误?

【问题讨论】:

    标签: tcl throw


    【解决方案1】:

    对于像您的示例这样的琐碎情况,请使用 on error 处理程序,而不是 trap。或者使用catch 而不是try

    例如tclsh 会话:

    % try { glob *.bar } on error {what} { puts "Ooops: $what" }
    Ooops: no files matched glob pattern "*.bar"
    % if {[catch { glob *.bar } result] == 1} { puts "Ooops: $result" }
    Ooops: no files matched glob pattern "*.bar"
    

    或者,如果您确实想使用trap,因为您还想处理来自更复杂代码的一系列其他可能的特定错误,glob 会在失败时引发TCL OPERATION GLOB NOMATCH

    % try { glob *.bar } trap {TCL OPERATION GLOB NOMATCH} {msg} { puts "Ooops: $msg" }
    Ooops: no files matched glob pattern "*.bar"
    

    您可以通过trap 发现针对任何给定命令的特定错误使用的内容,例如:

    % catch { glob *.bar } result errdict
    1
    % dict get $errdict -errorcode
    TCL OPERATION GLOB NOMATCH
    

    【讨论】:

      【解决方案2】:

      在这个特定的情况下,glob 有一个可以提供帮助的选项:-nocomplain。它会在不匹配时关闭错误——这只是为了交互式使用——因为许多用例可以很好地处理一个空的返回列表。 (由于历史原因,它一直采用这种方式,因此我们不会破坏使用它的大量现有脚本。随着语言疣的发展,它并不算太糟糕。)

      set latest_submission_folder [glob -directory $submission_parent -type d *]
      if {![llength $latest_submission_folder]} {
          # Make your own handling right here. Whatever you want.
          puts "I didn't find a directory inside $submission_parent"
      } elseif {[llength $latest_submission_folder] > 1} {
          # Many subdirectories found. Is this an error given your single-thing var name?
      } else {
          # Strip a layer of list; good idea in case directory name has a space in it!
          set latest_submission_folder [lindex $latest_submission_folder 0]
      }
      

      【讨论】:

      • 我认为您忘记了示例中的-nocomplain
      • 我喜欢这个答案,但如果找不到文件夹,我想停止程序。我更新了我的问题中的代码。我找到了最新的提交文件夹,如果不存在,那么我会抛出一个错误,因为我需要使用该文件夹,如果没有这个文件夹,程序就无法继续。
      猜你喜欢
      • 2011-06-18
      • 2019-04-02
      • 1970-01-01
      • 1970-01-01
      • 2012-07-12
      • 2014-09-28
      • 2015-09-10
      • 1970-01-01
      相关资源
      最近更新 更多