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

Content added Content deleted
m (→‎{{header|J}}: add headings and whitespace)
(→‎{{header|J}}: less concise but clearer?)
Line 379: Line 379:
J leaves most of the implementation of exceptions to the programmer, so:
J leaves most of the implementation of exceptions to the programmer, so:


<lang J>foo=: monad define
<lang J>main=: monad define
smoutput 'main'
try. foo ''
catcht. smoutput 'main caught ',type_jthrow_
end.
)

foo=: monad define
smoutput ' foo'
for_i. 0 1 do.
for_i. 0 1 do.
try. bar i
try. bar i
catcht. if. type_jthrow_-:'U0' do. smoutput 'caught U0' else. throw. end.
catcht. if. type_jthrow_-:'U0' do. smoutput ' foo caught ',type_jthrow_ else. throw. end.
end.
end.
end.
end.
)
)


bar=: baz
bar=: baz [ smoutput bind ' bar'


baz=: monad define
baz=: monad define
smoutput ' baz'
type_jthrow_=: 'U',":y throw.
type_jthrow_=: 'U',":y throw.
)</lang>
)</lang>


'''Example use:'''
'''Example use:'''
<lang j> main ''

main
foo 9
foo
caught U0
type_jthrow_
bar
baz
U1
foo caught U0
bar
baz
main caught U1</lang>


=={{header|Java}}==
=={{header|Java}}==