{"id":5949,"date":"2022-12-20T18:57:46","date_gmt":"2022-12-20T21:57:46","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/try-mann\/"},"modified":"2022-12-20T18:57:46","modified_gmt":"2022-12-20T21:57:46","slug":"try-mann","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/try-mann\/","title":{"rendered":"try (mann)"},"content":{"rendered":"<h1 align=\"center\">try<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#KEYWORDS\">KEYWORDS<\/a> <\/p>\n<hr>\n<p>______________________________________________________________________________<\/p>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">try \u2212 Trap and process errors and exceptions<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>try<\/b> <i>body<\/i> ?<i>handler&#8230;<\/i>? ?<b>finally<\/b> <i>script<\/i>? ______________________________________________________________________________<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This command executes the script <i>body<\/i> and, depending on what the outcome of that script is (normal exit, error, or some other exceptional result), runs a handler script to deal with the case. Once that has all happened, if the <b>finally<\/b> clause is present, the <i>script<\/i> it includes will be run and the result of the handler (or the <i>body<\/i> if no handler matched) is allowed to continue to propagate. Note that the <b>finally<\/b> clause is processed even if an error occurs and irrespective of which, if any, <i>handler<\/i> is used.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>handler<\/i> clauses are each expressed as several words, and must have one of the following forms: <b><br \/> on<\/b> <i>code variableList script<\/i><\/p>\n<p style=\"margin-left:22%;\">This clause matches if the evaluation of <i>body<\/i> completed with the exception code <i>code<\/i>. The <i>code<\/i> may be expressed as an integer or one of the following literal words: <b>ok<\/b>, <b>error<\/b>, <b>return<\/b>, <b>break<\/b>, or <b>continue<\/b>. Those literals correspond to the integers 0 through 4 respectively.<\/p>\n<p style=\"margin-left:11%;\"><b>trap<\/b> <i>pattern variableList script<\/i><\/p>\n<p style=\"margin-left:22%;\">This clause matches if the evaluation of <i>body<\/i> resulted in an error and the prefix of the <b>\u2212errorcode<\/b> from the interpreter\u2019s status dictionary is equal to the <i>pattern<\/i>. The number of prefix words taken from the <b>\u2212errorcode<\/b> is equal to the list-length of <i>pattern<\/i>, and inter-word spaces are normalized in both the <b>\u2212errorcode<\/b> and <i>pattern<\/i> before comparison.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>variableList<\/i> word in each <i>handler<\/i> is always interpreted as a list of variable names. If the first word of the list is present and non-empty, it names a variable into which the result of the evaluation of <i>body<\/i> (from the main <b>try<\/b>) will be placed; this will contain the human-readable form of any errors. If the second word of the list is present and non-empty, it names a variable into which the options dictionary of the interpreter at the moment of completion of execution of <i>body<\/i> will be placed.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>script<\/i> word of each <i>handler<\/i> is also always interpreted the same: as a Tcl script to evaluate if the clause is matched. If <i>script<\/i> is a literal \u201c\u2212\u201d and the <i>handler<\/i> is not the last one, the <i>script<\/i> of the following <i>handler<\/i> is invoked instead (just like with the <b>switch<\/b> command).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note that <i>handler<\/i> clauses are matched against in order, and that the first matching one is always selected. At most one <i>handler<\/i> clause will selected. As a consequence, an <b>on error<\/b> will mask any subsequent <b>trap<\/b> in the <b>try<\/b>. Also note that <b>on error<\/b> is equivalent to <b>trap {}<\/b>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If an exception (i.e. any non-<b>ok<\/b> result) occurs during the evaluation of either the <i>handler<\/i> or the <b>finally<\/b> clause, the original exception\u2019s status dictionary will be added to the new exception\u2019s status dictionary under the <b>\u2212during<\/b> key.<\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Ensure that a file is closed no matter what:<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">set f [open \/some\/file\/name a] <b><br \/> try<\/b> { <br \/> puts $f &#8220;some message&#8221; <br \/> # &#8230; <br \/> } <b>finally<\/b> { <br \/> close $f <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Handle different reasons for a file to not be openable for reading:<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\"><b>try<\/b> { <br \/> set f [open \/some\/file\/name w] <br \/> } <b>trap<\/b> {POSIX EISDIR} {} { <br \/> puts &#8220;failed to open \/some\/file\/name: it\u2019s a directory&#8221; <br \/> } <b>trap<\/b> {POSIX ENOENT} {} { <br \/> puts &#8220;failed to open \/some\/file\/name: it doesn\u2019t exist&#8221; <br \/> }<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">catch(n), error(n), return(n), throw(n)<\/p>\n<h2>KEYWORDS <a name=\"KEYWORDS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">cleanup, error, exception, final, resource management<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  try \u2212 Trap and process errors and exceptions <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3783,1],"tags":[2635,2644],"class_list":["post-5949","post","type-post","status-publish","format-standard","hentry","category-n-comandos-tcl-tk","category-sin-categoria","tag-mann","tag-try"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/5949","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=5949"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/5949\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=5949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=5949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=5949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}