Jump to content

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

add Ruby
(C# implementation)
(add Ruby)
Line 367:
 
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|Ruby}}==
Uses a global variable to count the number of calls to baz
<lang ruby>def foo
begin
bar
rescue U0
puts "captured exception U0"
end
end
 
def bar
baz
end
 
def baz
raise $bazcount == 1 ? U0 : U1
end
 
class U0 < Exception
end
 
class U1 < Exception
end
 
for $bazcount in [1, 2]
foo
end</lang>
<pre>$ ruby nested_calls.rb
captured exception U0
nested_calls.rb:14:in `baz': U1 (U1)
from nested_calls.rb:10:in `bar'
from nested_calls.rb:3:in `foo'
from nested_calls.rb:24
from nested_calls.rb:23:in `each'
from nested_calls.rb:23</pre>
Wait, why does <code>in `each'</code> appear in the stack trace? There's no each in that code. Ruby translates this
<lang ruby>for $bazcount in [1, 2]
foo
end</lang>
to this
<lang ruby>[1, 2].each {|$bazcount| foo}</lang>
 
=={{header|Tcl}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.