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

Content added Content deleted
Line 640: Line 640:
At this point, the debugger (if any) is invoked
At this point, the debugger (if any) is invoked
with the unhandled condition of type USER-CONDITION-2.
with the unhandled condition of type USER-CONDITION-2.

=={{header|Crystal}}==

<lang ruby>class U0 < Exception
end

class U1 < Exception
end

def foo
2.times do |i|
begin
bar(i)
rescue e : U0
puts "rescued #{e}"
end
end
end

def bar(i : Int32)
baz(i)
end

def baz(i : Int32)
raise U0.new("this is u0") if i == 0
raise U1.new("this is u1") if i == 1
end

foo</lang>

<pre>
rescued this is u0
Unhandled exception: this is u1 (U1)
from exceptions_nested.cr:28:2 in 'baz'
from exceptions_nested.cr:23:2 in 'bar'
from exceptions_nested.cr:15:7 in 'foo'
from exceptions_nested.cr:31:1 in '__crystal_main'
from /usr/local/Cellar/crystal/0.32.1/src/crystal/main.cr:97:5 in 'main_user_code'
from /usr/local/Cellar/crystal/0.32.1/src/crystal/main.cr:86:7 in 'main'
from /usr/local/Cellar/crystal/0.32.1/src/crystal/main.cr:106:3 in 'main'
</pre>


=={{header|D}}==
=={{header|D}}==