Name

try — Catch error and exception conditions

Synopsis

::rivet::try ?script? ?script? ?handlers? ?finally script?

Description

::rivet::try wraps the core language command and simply traps exceptions that might have raised by ::rivet::abort_page and ::rivet::exit to throw them again and thus causing AbortScript to be executed.

If neither ::rivet::abort_page nor ::rivet::exit are called from ?script? then any handlers specified in the command are tested for execution. Thus ::rivet::try can transparently be used as a replacement for Tcl's own try and it's needed if you want ?script? to safely bail out to AbortScript

[Note]Note
This command is not exported from the ::rivet namespace and therefore has to be fully qualified.

This script shows how ::rivet:try handles different exceptions or errors. You can drive this script within mod_rivet adding the arguments fail or abort or exit to its URL. You can handle the exit and abort cases with an AbortScript. See the section called “Apache Rivet 3.1 Configuration”

<html><?
::rivet::try {
	if {[::rivet::var_qs exists exit]} {
	    ::rivet::exit [::rivet::var_qs get exit]
	} elseif {[::rivet::var_qs exists abort]} {
	    ::rivet::abort_page [::rivet::var_qs get abort]
	} elseif {[::rivet::var_qs exists fail]} {
	    # this is just a non existent command
	    wrong_command
	} else {
	    puts "<b>OK</b>"
	}

} on error {e o} {
  puts "catching error -&gt; $e<br/>"
  dict for {fd fv} $o {

   puts "$fd --&gt;&gt; $fv<br/>"

  }
}
?></html>

Placing this code in a file (try.rvt) on the web server DocumentRoot directory and setting for example the browser to http://localhost/try.rvt?fail=1.

catching error -> invalid command name "wrong_command"
-errorcode -->> TCL LOOKUP COMMAND wrong_command
-code -->> 1
-level -->> 0
-errorstack -->> INNER {invokeStk1 wrong_command} UP 1
-errorinfo -->> invalid command name "wrong_command" while executing "wrong_command" ("::try" body line 9)
-errorline -->> 9