Exception 期望脚本引发异常

Exception 期望脚本引发异常,exception,error-handling,tcl,expect,Exception,Error Handling,Tcl,Expect,我正在尝试学习如何使用脚本来运行后端进程,有没有办法从这种语言中引发并捕获错误异常 例如Python 试试看: 引发异常(“测试”) 除例外情况外,e: 打印e expect中的等价物是什么 #!/usr/bin/expect package require Expect # raise and catch exception 在TCL中,您可以使用catch捕获异常: if {[catch { package require Expect } errmsg]} { puts "I

我正在尝试学习如何使用脚本来运行后端进程,有没有办法从这种语言中引发并捕获错误异常

例如Python

试试看:
引发异常(“测试”)
除例外情况外,e:
打印e
expect中的等价物是什么

#!/usr/bin/expect
package require Expect

# raise and catch exception

在TCL中,您可以使用
catch
捕获异常:

if {[catch { package require Expect } errmsg]} {
    puts "Import failed with message: $errmsg"
} else {
    puts "Import succeeded"
}
要引发异常,请使用
返回-code error
命令。例如:

proc double {x} {
    if {![string is integer $x]} {
        return -code error "double expects an int, not '$x'"
    }

    return [expr {$x * 2}]
}

set x 5
puts "x = $x"
puts "2x = [double $x]"

set x "Hello"
puts "x = $x"
if {[catch {puts "2x = [double $x]"} errmsg]} {
    puts "Error: $errmsg"
}
输出:

x = 5
2x = 10
x = Hello
Error: double expects an int, not 'Hello'

在TCL中,您可以使用
catch
捕获异常:

if {[catch { package require Expect } errmsg]} {
    puts "Import failed with message: $errmsg"
} else {
    puts "Import succeeded"
}
要引发异常,请使用
返回-code error
命令。例如:

proc double {x} {
    if {![string is integer $x]} {
        return -code error "double expects an int, not '$x'"
    }

    return [expr {$x * 2}]
}

set x 5
puts "x = $x"
puts "2x = [double $x]"

set x "Hello"
puts "x = $x"
if {[catch {puts "2x = [double $x]"} errmsg]} {
    puts "Error: $errmsg"
}
输出:

x = 5
2x = 10
x = Hello
Error: double expects an int, not 'Hello'

在TCL中,您可以使用
catch
捕获异常:

if {[catch { package require Expect } errmsg]} {
    puts "Import failed with message: $errmsg"
} else {
    puts "Import succeeded"
}
要引发异常,请使用
返回-code error
命令。例如:

proc double {x} {
    if {![string is integer $x]} {
        return -code error "double expects an int, not '$x'"
    }

    return [expr {$x * 2}]
}

set x 5
puts "x = $x"
puts "2x = [double $x]"

set x "Hello"
puts "x = $x"
if {[catch {puts "2x = [double $x]"} errmsg]} {
    puts "Error: $errmsg"
}
输出:

x = 5
2x = 10
x = Hello
Error: double expects an int, not 'Hello'

在TCL中,您可以使用
catch
捕获异常:

if {[catch { package require Expect } errmsg]} {
    puts "Import failed with message: $errmsg"
} else {
    puts "Import succeeded"
}
要引发异常,请使用
返回-code error
命令。例如:

proc double {x} {
    if {![string is integer $x]} {
        return -code error "double expects an int, not '$x'"
    }

    return [expr {$x * 2}]
}

set x 5
puts "x = $x"
puts "2x = [double $x]"

set x "Hello"
puts "x = $x"
if {[catch {puts "2x = [double $x]"} errmsg]} {
    puts "Error: $errmsg"
}
输出:

x = 5
2x = 10
x = Hello
Error: double expects an int, not 'Hello'
python示例的直译是

set status [catch {error "test"} e]
if {$status} {
    puts $e
}
这是Tcl 8.5,python示例的直译是

set status [catch {error "test"} e]
if {$status} {
    puts $e
}
这是Tcl 8.5,python示例的直译是

set status [catch {error "test"} e]
if {$status} {
    puts $e
}
这是Tcl 8.5,python示例的直译是

set status [catch {error "test"} e]
if {$status} {
    puts $e
}
这是Tcl 8.5

如果您使用的是Tcl 8.6(Expect软件包将很好地加载到该软件中),则该Python代码的最直译为:

try {
    throw Exception "test"
} trap Exception e {
    puts $e
}
现在,在8.6中添加了
try
throw
命令,以简化这类操作。在此之前(从远比我能方便地搜索到的更远的地方),你会做这样的事情:

if {[catch {
    error "test"
} e] == 1} {
    puts $e
}
在这种情况下,这很容易,但一旦事情变得更复杂,就更容易出错。

如果您使用的是Tcl 8.6(Expect软件包将很好地加载到该软件中),Python代码的最直译为:

try {
    throw Exception "test"
} trap Exception e {
    puts $e
}
现在,在8.6中添加了
try
throw
命令,以简化这类操作。在此之前(从远比我能方便地搜索到的更远的地方),你会做这样的事情:

if {[catch {
    error "test"
} e] == 1} {
    puts $e
}
在这种情况下,这很容易,但一旦事情变得更复杂,就更容易出错。

如果您使用的是Tcl 8.6(Expect软件包将很好地加载到该软件中),Python代码的最直译为:

try {
    throw Exception "test"
} trap Exception e {
    puts $e
}
现在,在8.6中添加了
try
throw
命令,以简化这类操作。在此之前(从远比我能方便地搜索到的更远的地方),你会做这样的事情:

if {[catch {
    error "test"
} e] == 1} {
    puts $e
}
在这种情况下,这很容易,但一旦事情变得更复杂,就更容易出错。

如果您使用的是Tcl 8.6(Expect软件包将很好地加载到该软件中),Python代码的最直译为:

try {
    throw Exception "test"
} trap Exception e {
    puts $e
}
现在,在8.6中添加了
try
throw
命令,以简化这类操作。在此之前(从远比我能方便地搜索到的更远的地方),你会做这样的事情:

if {[catch {
    error "test"
} e] == 1} {
    puts $e
}
在这种情况下这很容易,但一旦事情变得更复杂,就更容易出错