코틀린의 Error Handling - runCatching
·
스터디/Kotlin
코틀린의 runCatching자바의 try~catch 와 동일하게 지원하지만 조금 다른 방법으로 핸들링 할 수도 있습니다! 예를들어 아래와 같은 요구사항이 있다고 가정해봅니다.LoginApiClient 호출 시 LoginException 발생errorCode = INVALID_PASSWORD인 경우 예외를 발생하지 않고 null을 리턴한다.그 외 에러는 모두 예외를 발생시킨다. try ~ catch 를 사용한 코드try { loginApiClient.login(request)} catch (e: LoginException) { if (e.errorCode == "INVALID_PASSWORD") { return null } else { throw e }} Java 에서는 익숙하게 위처..