Skip to content

Instantly share code, notes, and snippets.

@CheolhoJeon
Created June 18, 2021 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CheolhoJeon/5b5863f56c54ad2017217682d0239561 to your computer and use it in GitHub Desktop.
Save CheolhoJeon/5b5863f56c54ad2017217682d0239561 to your computer and use it in GitHub Desktop.
package chap6.ExceptionHandling
import atomictest.eq
import java.lang.Exception
fun toss(which: Int) = when (which) {
1 -> throw Exception1(1)
2 -> throw Exception2("Exception 2")
3 -> throw Exception3("Exception 3")
else -> "OK"
}
fun test(which: Int): Any? =
try {
toss(which)
} catch (e: Exception1) {
e.value
} catch (e: Exception3) {
e.message
} catch (e: Exception2) {
e.message
}
fun main() {
test(0) eq "OK"
test(1) eq 1
test(2) eq "Exception 2"
test(3) eq "Exception 3"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment