Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
try-catchからrunCatchingに_移行した話.pdf
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
yuki anzai
August 24, 2019
Programming
3.4k
6
Share
try-catchからrunCatchingに_移行した話.pdf
yuki anzai
August 24, 2019
More Decks by yuki anzai
See All by yuki anzai
5分でざっくり理解する Jetpack Compose
kuromame
0
250
Flux + Repositoryへリアーキテクチャ後にテストを書き始めて1ヶ月経った話
kuromame
4
360
Other Decks in Programming
See All in Programming
次世代リンターで探る、tsgo 時代における型認識カスタムルールの現実解
ytakahashii
3
1.3k
Spec-Driven Development with AI-Agents: From High-Level Requirements to Working Software
antonarhipov
2
400
tsserverとは何だったのか、これからどうなるのか
nowaki28
1
430
AI駆動開発で崩れていくコードベースを立て直す
kyoko_nr_nr
1
400
1人1案件のプロダクトエンジニア時代に、"プロセス監督"としてチャレンジしたこと
non0113
0
360
Why Laravel apps break—Mastering the fundamentals to keep them maintainable
kentaroutakeda
1
320
ふつうのFeature Flag実践入門
irof
7
3.4k
密結合なバックエンドから TypeScript のコードを生成する
kemuridama
1
390
誰も頼んでない機能を出荷した話
zekutax
0
150
TypeSpec で繋ぐ複数プロダクトの型安全
maroon8021
1
270
iOS26時代の新規アプリ開発
yuukiw00w
0
220
Stage 3 Decorators でできること / できないこと / TSKaigi 2026
susisu
1
1.4k
Featured
See All Featured
Heart Work Chapter 1 - Part 1
lfama
PRO
7
36k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
410
GraphQLとの向き合い方2022年版
quramy
50
15k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
190
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
830
Measuring & Analyzing Core Web Vitals
bluesmoon
9
850
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
200
Designing Powerful Visuals for Engaging Learning
tmiket
1
390
Side Projects
sachag
455
43k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
150
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
231
23k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
130
Transcript
try-catchからrunCatchingに 移行した話 安齋祐紀 (@off2white)
自己紹介 安齋祐紀(あんざいゆうき) Twitter: @off2white 株式会社 ディー・エヌ・エー(DeNA) - 次世代タクシー配車サービス「 MOV」 -
Androidアプリ開発担当 - プロジェクト管理とコーディングの割合 = 50:50 (気持ちは) 最近の悩み いまだに運営さんが採択時に 人を間違えていなかったのか心配
None
背景 個人的エラーハンドリング の変遷
その1 むやみに例外を使わなくなった
ClassA ClassB ClassC Exception New ClassD 誰が Catch する? NewException
Not For Me Exception ?
ClassA ClassB ClassC sealed class New ClassD I Like sealed
class sealed class
ViewModel Repository Api Exception New DB ただし現実的にはこんな感じ sealed class Exception
その2 Crashlyticsの発達によって 細かくExceptionをCatchする必要が なくなった
蘇る悪夢 try { response = apiRequest.execute() dao.save(response.toEntity()) }
catch (e: IOException) { errorCode += “01” throw NetworkException(errorCode) } catch (e: SQLException) { errorCode += “03” throw GeneralException(errorCode) } catch (e: Throwable) { errorCode += “04” throw SystemException(errorCode) }
その3 Rx -> Coroutineへの移行
Rx では success と error 時の処理を 分けて記載できた repository.fetchData() .subscribeBy
( onNext = { livedata.postValue(it)}, onError = { Timber.e(it) } ) 実 行 系 正 常 系 異 常 系
もっと良いエラーハンドリングの書き方はないのか
runCatching (Kotlin 1.3)
runCatching { apiRequest.execute() } or apiRequest.execute()
.runCatching { dao.save(it.toEntity()) } こんな感じで書く
runCatchingは Resultクラスを返却する
成功結果と例外を カプセル化してくれる
val result = runCatching { apiRequest.execute() } if (result.isFailure)
{ Timber.e( result.exceptionOrNull() ) return } 処理結果を受け取って 返却してくれる
runCatching { apiRequest.execute() }.onSuccess { dao.save(it.toEntity()) }.onFailure { Timber.e(it) }
成功処理と失敗処理を 分けて記載できる 実 行 系 正 常 系 異 常 系
runCatching { apiRequest.execute() }.mapCatching { dao.save(it.toEntity()) }.onSucess { … }.onFailure
{ ... } map 時も Catch できる
runCatching { apiRequest.execute() }.recoverCatching { Response.default() } 例外処理のリカバリ処 理も綺麗にかける
これは是非使うべき!!!
結論 案外不評
try { res = apiClient.execute() dao.save(res.toEntity()) } catch (e: Exception)
{ Timber.e(e) } 正常パスと 例外処理で 別れている方が 好みの人もいる 正 常 パ ス 例 外 処 理 理由その1
val a: Int? = try { parseInt(input) } catch
(e: Exception) { null } Kotlin の try-catch は式 として書けるので それで十分説 理由その2
val a: Int? = try { parseInt(input) } catch
(e: Exception) { null } finally { ... } try-catch なら finally で 明示的に書ける (runCatching ではできない ) 理由その3
return runCatching { … } Result 型は return できない 理由その4
fun function() : Int { runCatching { "5".toInt() }.onSuccess {
return@function it }.onFailure { return@function 0 } // ここにreturnが必要 } a ‘return’ expression required in a function with a block body 理由その5
(;Ծ﹏Ծ)ぐぬぬ
runCatching { runFunction() }.onSuccess { dispatch(Action.Success) }.onFailure { dispatch(Action.Failure) }
現状は return しない ところで そっと使っている ActionCreator の dispatch とか ViewModel の LiveData.postValue とか
Resultクラスの KEEPでの議論が面白いので ぜひ読んでね!