Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Pitfalls of Cancellation

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

The Pitfalls of Cancellation

Avatar for Masatoshi Kubode

Masatoshi Kubode

December 22, 2020
Tweet

More Decks by Masatoshi Kubode

Other Decks in Programming

Transcript

  1. ©2020 Wantedly, Inc. The Pitfalls of Cancellation Presentation Subtitle 2020/12/22

    Masatoshi Kubode https://unsplash.com/photos/J8sh37XZ8ww
  2. ©2020 Wantedly, Inc. ֘౰ίʔυ class FetchDiscoverPostsUseCaseImpl( private val discoverRepository: DiscoverRepository,

    ) : FetchDiscoverPostsUseCase { override suspend operator fun invoke(sectionId: DiscoverSectionId) { try { return discoverRepository.fetchDiscoverPosts(sectionId) } catch (@Suppress("TooGenericExceptionCaught") e: Throwable) { throw FetchDiscoverProjectsUseCase.Error( "Failed to fetch discover posts for section: $sectionId”, e ) } } } ←ίϐϖͰTypoͯ͠Δ Repositoryͷྫ֎Λϥοϓͯ͠ Rethrowͯ͠Δ͚ͩͳͷʹ ͳͥ?
  3. ©2020 Wantedly, Inc. ֘౰ίʔυ class FetchDiscoverPostsUseCaseImpl( private val discoverRepository: DiscoverRepository,

    ) : FetchDiscoverPostsUseCase { override suspend operator fun invoke(sectionId: DiscoverSectionId) { try { return discoverRepository.fetchDiscoverPosts(sectionId) } catch (@Suppress("TooGenericExceptionCaught") e: Throwable) { throw FetchDiscoverProjectsUseCase.Error( "Failed to fetch discover posts for section: $sectionId”, e ) } } } ͭ·ΓɺCoroutinesͷγεςϜతͳྫ֎Λ ϥοϓͯ͠͠·͔ͬͨΒΫϥογϡͨ͠
  4. ©2020 Wantedly, Inc. Javaͷੈք java.util.concurrent.Future#get() try { future.get(); } catch

    (ExecutionException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } CancellationException͸RuntimeException ͳͷͰ໌ࣔతͳcatchෆཁ CancellationΛcatchͪ͠Ό͏͜ͱ͕ى͖ͮΒ͍
  5. ©2020 Wantedly, Inc. Ͳ͏͢΂͖͔ launch { try { codeThatCanThrowExceptions() //

    // } catch (e: Throwable) { // } catch (e: Exception) { // } catch (e: RuntimeException) { // } catch (e: IllegalStateException) { // } catch (e: CancellationException) { } catch (e: SpecificException) { // throw MyException(e) } } // OR launch { try { codeThatCanThrowExceptions() } catch (e: Throwable) { throw e as? CancellationException ?: MyException(e) } }