Error handling in TCL
Asked Answered
C

1

5

I have a following procedure:

proc show_information {args} {
set mandatory_args {
        -tftp_server
        -device ANY
        -interface ANY

}
set optional_args {
        -vlan ANY

}

parse_dashed_args -args $args -optional_args $optional_args -mandatory_args $mandatory_args

log -info "Logged in device is: $device"
log -info "Executing proc to get all data"
set commands {
              "show mpls ldp neighbor"
              "show mpls discovery vpn"
              "show mpls interface"
              "show mpls ip binding"
              "show running-config"
              "show policy-map interface $intr vlan $vlan input"
              "show run interface $intr
            }

foreach command $commands {
    set show [$device exec $command]
    ats_log -info $show

  }
 }

I am new to tcl and wants to know how can we handle error if anyhow we pass wrong parameter or it errors out. Something like python try *(executes the proc) and except *(prints some msg in case of failure) in TCL.

After some googling 'catch' is what is used in TCL but not able to figure out how can I use it.

Cheerio answered 4/4, 2017 at 4:50 Comment(0)
P
11

The catch command runs a script and catches any failures in it. The result of the command is effectively a boolean that describes whether an error occurred (it's actually a result code, but 0 is success and 1 is error; there are a few others but you won't normally encounter them). You can also give a variable into which the “result” is placed, which is the normal result on success and the error message on failure.

set code [catch {
    DoSomethingThat mightFail here
} result]

if {$code == 0} {
    puts "Result was $result"
} elseif {$code == 1} {
    puts "Error happened with message: $result"
} else {
    # Consult the manual for the other cases if you care
    puts "Code: $code\nResult:$result"
}

In many simple cases, you can shorten that to:

if {[catch {
    DoSomethingThat mightFail here
} result]} {
    # Handle the error
} else {
    # Use the result
}

Tcl 8.6 added a new command for handling errors though. The try command is rather more like what you're used to with Python:

try {
    DoSomethingThat mightFail here
} on error {msg} {
    # Handle the error
}

It also supports finally and trap clauses for guaranteed actions and more sophisticated error handling respectively. For example (something which it is annoying to write with catch):

try {
    DoSomethingThat mightFail here
} trap POSIX {msg} {
    # Handle an error from the operating system
}
Peroxy answered 4/4, 2017 at 5:32 Comment(3)
Thanks Donal (please correct if wrong): try { show_information {some arguments} } { print "check your arguments" }Cheerio
try { show_information {some arguments} } on error {msg} { # Handle the error puts "Error $msg" }Progressive
I think better use error to trow errors. Or return has additional parameters for that. return -code error "my error"Kortneykoruna

© 2022 - 2024 — McMap. All rights reserved.