2019年4月18日木曜日

開発環境

Head First Kotlin: A Brain-Friendly Guide (Dawn Griffiths(著)、David Griffiths(著)、O'Reilly Media)のChapter 8(nulls and exceptions - Safe and Sound)、Code Magnets(247)の解答を求めてみる。

コード

class BadException : Exception()

fun myFunction(test: String) {
    try {
        print("t")
        print("h")
        riskyCode(test)
    } catch (e: BadException) {
        print("a")
    } finally {
        print("w")
        print("s")
    }
}

fun riskyCode(test: String) {
    if (test == "Yes") {
        throw BadException()
    }
    print("r")
    print("o")
}

fun main() {
    val tests = arrayOf("Yes", "No")
    for (test in tests) {
        println(test)
        myFunction(test)
        println()
    }
}

入出力結果

Yes
thaws
No
throws

Process finished with exit code 0

0 コメント:

コメントを投稿