下面的代碼是從官方的樣本工程中的噸。
啟動IllegalArgumentExceptionR 時會終止應用程式esult.Error(IllegalArgumentException("City doesn't exist"))嗎?
sealed class Result<out R> {
data class Success<out T>(val data: T) : Result<T>()
data class Error(val exception: Exception) : Result<Nothing>()
}
class DetailsViewModel @Inject constructor(
private val destinationsRepository: DestinationsRepository,
savedStateHandle: SavedStateHandle
) : ViewModel() {
private val cityName = savedStateHandle.get<String>(KEY_ARG_DETAILS_CITY_NAME)!!
val cityDetails: Result<ExploreModel>
get() {
val destination = destinationsRepository.getDestination(cityName)
return if (destination != null) {
Result.Success(destination)
} else {
Result.Error(IllegalArgumentException("City doesn't exist")) // Will IllegalArgumentException termination an app ?
}
}
}
uj5u.com熱心網友回復:
在這種情況下,應用程式不會崩潰(終止),因為IllegalArgumentException例外只是被創建,而不是被拋出。如果拋出例外并且未被try-catch操作員捕獲,應用程式將崩潰(終止),如下所示:
throw IllegalArgumentException("City doesn't exist")
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/405912.html
標籤:
上一篇:未能找到包含/storage/708A-1A0F/-的已配置根目錄
下一篇:為什么會出現這種奇怪的輸出?
