開発環境
- macOS Mojave - Apple (OS)
 - Windows 10 Pro (OS)
 - IntelliJ IDEA CE(Community Edition) (IDE(統合開発環境))
 - Kotlin (プログラミング言語)
 
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 コメント:
コメントを投稿