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

Content deleted Content added
Sonia (talk | contribs)
→‎{{header|Go}}: generalized solution, added text and comments
Line 875:
<pre>caught exception U0
uncaught exception: U1</pre>
 
=={{header|Lua}}==
<lang Lua>local baz_counter=1
function baz()
if baz_counter==1 then
baz_counter=baz_counter+1
error("U0",3)--3 sends it down the call stack.
elseif baz_counter==2 then
error("U1",3)--3 sends it down the call stack.
end
end
 
function bar()
baz()
end
 
function foo()
function callbar()
local no_err,result = pcall(bar)
--pcall is a protected call which catches errors.
if not no_err then
--If there are no errors, pcall returns true.
if not result:match("U0") then
--If the error is not a U0 error, rethrow it.
error(result,2)
--2 is the distance down the call stack to send
--the error. We want it to go back to the callbar() call.
end
end
end
callbar()
callbar()
end
 
foo()
</lang>
output:
<pre>lua: errorexample.lua:31: U1
stack traceback:
[C]: in function 'error'
errorexample.lua:24: in function 'callbar'
errorexample.lua:31: in function 'foo'
errorexample.lua:34: in main chunk
[C]: ?</pre>
 
=={{header|MATLAB}}==