Jump to content

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

added swift
m (→‎{{header|Sidef}}: minor fix)
(added swift)
Line 2,333:
 
CatchMeIfYouCan new foo</lang>
 
=={{header|Swift}}==
{{works with|Swift|2.x+}}
<lang swift>enum MyException : ErrorType {
case U0
case U1
}
 
func foo() throws {
for i in 0 ... 1 {
do {
try bar(i)
} catch MyException.U0 {
print("Function foo caught exception U0")
}
}
}
 
func bar(i: Int) throws {
try baz(i) // Nest those calls
}
 
func baz(i: Int) throws {
if i == 0 {
throw MyException.U0
} else {
throw MyException.U1
}
}
 
try foo()</lang>
{{out}}
<pre>
Function foo caught exception U0
error caught in main()
</pre>
 
=={{header|Tcl}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.