Exceptions/Catch an exception thrown in a nested call: Difference between revisions

Content added Content deleted
(added C++ implementation)
No edit summary
Line 235:
 
Uncaught exceptions give information showing where the exception originated through the nested function calls together with the name of the uncaught exception, (U1) to stderr, then quit the running program.
 
=={{header|Tcl}}==
{{works with|Tcl|8.5}}
<lang tcl>package require Tcl 8.5
 
proc foo {} {
set code [catch {bar} ex options]
if {$code == 1} {
switch -exact -- $ex {
U0 {puts "caught exception U0"}
default {return -options $options $ex ;# re-raise exception}
}
}
}
 
proc bar {} {baz}
 
# create an alias to pass the initial exception U0 to the baz proc
interp alias {} baz {} _baz U0
 
proc _baz {exception} {
# re-set the alias so subsequent invocations will use exception U1
interp alias {} baz {} _baz U1
# throw
return -code error $exception
}
 
foo
foo</lang>
Running this program results in:
<pre>$ tclsh85 exceptions.tcl
caught exception U0
U1
while executing
"baz"
(procedure "bar" line 1)
invoked from within
"bar"
(procedure "foo" line 2)
invoked from within
"foo"
(file "exceptions.tcl" line 26)</pre>