LoginSignup
0
1

More than 3 years have passed since last update.

初めてのkotlinを触る - Sealed Classes(2)

Posted at

目的

内容

例外処理の苦労点

  • kotlin言語には、チェック済み例外がない
  • ドキュメントにに明文化されず、発生しても誰にも気付かれない恐れがある

sealed classで解決できる

  • 例外を投げることを代わりに、違う結果インストールを返す(例:success, error...)
    • もし処理結果ケースが足りない場合は、コンパイラーは自動的に何か結果ケースが足りないことを通知できる
  • 結果ケースが同じファイルに定義されるので、読みやすくになる
  • javaぽいexceptionの定義

exceptionでの処理方法

fun requestWeatherIcon(id: String): WeatherIcon = try {
    API.getWeatherIcon(id)!!
} catch (ex: IOException) {
    throw IconClientException(
        message = "Io request failed",
        cause = ex
    )
} catch (ex: RestClientException) {
    throw IconClientException(
        message = "Server request failed. Id: $id.",
        cause = ex
    )
}

class IconClientException(message: String, cause: Exception? = null) : RuntimeException(message, cause)

sealedで結果ケースを定義

sealed class IconResult {
    data class Success(val icon: WeatherIcon) : IconResult()
    data class Error(val message: String, val cause: Exception? = null) : IconResult()
}


sealedで処理

fun requestWeatherIcon(id: String): IconResult = try {
    val iconProfile = API.getWeatherIcon(id)
    IconResult.Success(icon = iconProfile)
} catch (ex: IOException) {
    IconResult.Error(
        message = "Server request failed. Id: $id.",
        cause = ex
    )
} catch (ex: RestClientException) {
    IconResult.Error(
        message = "Server request failed. Id: $id.",
        cause = ex
    )
}

val icon = when (val result = requestWeatherIcon("3")) {
    is IconResult.Success -> result.icon
    is IconResult.Error -> "http://domain.com/defaulIcon.png"
}

足りない場合はcompile error

val icon = when (val result = requestWeatherIcon("3")) {
    is IconResult.Success -> result.icon
}
'when' expression must be exhaustive, add necessary 'is Error' branch or 'else' branch instead

参照

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1