2021년 3월 1일 월요일

[Kotlin] try, catch 대신 runcatch

 참고 사이트 : https://uchun.dev/runCatching%EC%9D%84-%EC%9D%B4%EC%9A%A9%ED%95%9C-kotlin%EC%97%90%EC%84%9C-exception%EC%B2%98%EB%A6%AC-%EB%B0%A9%EB%B2%95/

kotlin 에서 기존의 try catch 도 가능하지만 

val fruitName = try {

    getRandomFruit()

} catch (throwable: Throwable) {

    ""

}


ex 

return runCatching {
if (argParam.isEmpty()) {
method?.invoke(null) as T?
} else {
method?.invoke(obj, null) as T?
}

}.getOrNull()

코드가 정말 깔끔해진다

0 comments:

댓글 쓰기