2018年12月17日月曜日

Kotlin メモ : require(), check()

require

指定した condition を満たさなかった場合 IllegalArgumentException が throw されます。IllegalArgumentException に渡す message を第2引数の lambda で指定することができます。
  1. data class AccessKey(val value: String) {  
  2.     init {  
  3.         // value が empty の場合 IllegalArgumentException("value must not be empty") が throw される  
  4.         require(value.isNotEmpty()) { "value must not be empty" }  
  5.     }  
  6. }  



check

指定した condition を満たさなかった場合 IllegalStateException が throw されます。IllegalStateException に渡す message を第2引数の lambda で指定することができます。
  1. data class AccessKey(val value: String) {  
  2.     init {  
  3.         // value が empty の場合 IllegalStateException("value must not be empty") が throw される  
  4.         check(value.isNotEmpty()) { "value must not be empty" }  
  5.     }  
  6. }  



0 件のコメント:

コメントを投稿