Categorize ErrorHandling

3925ee6eaa41031bac799de0f4f528ec?s=47 to4iki
January 21, 2017

Categorize ErrorHandling

error-handling/exception mechanism tips

3925ee6eaa41031bac799de0f4f528ec?s=128

to4iki

January 21, 2017
Tweet

Transcript

  1. Categorize ErrorHandling shinjyuku.lt #4 @to4iki

  2. People • @to4iki • GANMA!ͱ͍͏ອըͷαʔϏεΛ࡞ͬͯ·͢ • iOSϝΠϯʹ։ൃ͍ͯ͠·͢(ͨ·ʹAndroid) • ࠷ۙ͸fastlane͓͡͞ΜΛͯ͠·͢ɻRubyॻ͍ ͯΔ

  3. Agenda • ͸͡Ίʹ • ྫ֎(Τϥʔ)ͷར఺ • Τϥʔछผͷ঺հ

  4. ͸͡Ίʹ

  5. ྫ֎ઃܭ͸೉͍͠

  6. ྫ֎ઃܭ͸೉͍͠ • Ͳͷཻ౓Ͱྫ֎Λ౤͛Δ΂͖͔ɺ͜Ε͸ຊ౰ʹྫ֎ͱͯ͠ѻ ͏΂͖ͳͷ͔ͷ൑அج४͕೉͍͠(ࠓճ͸͜ΕΛߟ͑ͯΈΔ) • ݴޠ΍จԽʹԠͯ͡ઃܭ͕ҟͳΔ • Java, Swift, Ruby:

    try~catch • Scala: Either[L, R], Try[T] • Go, C, JavaScript: ؔ਺ͷ໭Γ஋΍callbackҾ਺ͰerrorΛද ݱ • etc…
  7. ੒ޭ/ࣦഊ/όά/ྫ֎ http://qiita.com/Kokudori/items/3a953c00012408f76ab9

  8. ྫ֎(Τϥʔ)Λѻ͏ར఺

  9. Pros • “ྫ֎ͷ࠷େͷར఺͸ɺແࢹͰ͖ͳ͍ํ๏ͰΤ ϥʔঢ়ଶΛ஌ΒͤΔ͜ͱͰ͋Δ” • “ྫ֎ΛΩϟον͢Δओͳ໨త͸ɺΤϥʔͷݪ ҼΛऔΓআ͍ͯɺճ෮͢Δ͜ͱ” via .NET •

    ൃੜͨ͠Τϥʔͷ಺༰Λత֬ʹදݱͰ͖Δ • ൚༻తʹΤϥʔΛѻ͏͜ͱ͕ग़དྷΔ • Τϥʔͷѻ͍ํΛମܥԽͰ͖Δ => ίʔυ ʹҰ؏ੑΛ࣋ͨͤΔ͜ͱ͕ग़དྷΔ
  10. Cons • ຊདྷͷॲཧͷྲྀΕΛͿͬͨ੾ͬͯ”େҬ୤ग़”Ͱ͖͏Δ • ΦϒδΣΫτࢦ޲ͱͷ෇͖߹͍ํ͕೉͍͠ • લޙঢ়ଶΛ஌ࣝͱͯ࣋ͭ͠ඞཁ͕͋Δ • ୭ͷ੹೚ͱ͢Δ͔͕ •

    டংΛอͨͳ͍ͱΧΦε(खൈ͖͠Α͏ͱࢥ͑͹͍͘ΒͰ΋ग़དྷΔ) • ྫ֎͕ѲΓͭͿ͞ΕΔ • ͳΜͰ΋͔ΜͰ΋ྫ֎ʹแΉ
  11. Comparison: ໭Γ஋Ͱྫ֎Λѻ͏৔߹ • γϯϓϧ • ৑௕ͳνΣοΫํ๏ • ੩తͳνΣοΫͷԸܙΛड͚ΒΕͳ͍ • ࣮ߦ࣌ͷίετ

  12. શͯʹରͯ͠ྫ֎͸ສೳͰ͸ͳ͍

  13. Ͳ͏ྫ֎Λѻ͏ͷ͔ɺ ൑அج४ͷࢀߟͱͳΔ4෼ྨ http://qiita.com/koher/private/58cf39b09f4e43725572

  14. “ͲͷΑ͏ʹରԠ͍͔ͤͨ͞”ʹΑΔ4෼ྨ 1. Simple domain error 2. Recoverable error 3. Universal

    error 4. Logic failure
  15. None
  16. 1. Simple domain error • ࣦഊݪҼ͕໌നͳ৔߹ɺΤϥʔ͕ى͔ͬͨ͜Ͳ͏͔͑͞Θ͔ Ε͹Α͍ • ໰୊͕͋Δ͔/ͳ͍͔ •

    OptionalΛ࢖͏͜ͱͰදݱ͢Δ • ex. ਺஋ → จࣈྻͷม׵
  17. func toInt(s: String) -> Int? guard let i = toInt(s:

    "1") else { print("enter an integer") return } // execute using `i: Int`
  18. 2. Recoverable error • ݪҼʹΑͬͯॲཧ(ͲͷΑ͏ʹճ෮͢΂͖͔)ͷํ๏͕ҟͳΔͩ Ζ͏ͱߟ͑Δ৔߹ʹ࠾༻ • ࣦഊཧ༝Λ໌ه͢Δ • try~catch

    / Result(Either)Ͱදݱ͢Δ • ex. ϑΝΠϧI/OɺωοτϫʔΫΤϥʔ
  19. func loadFile(at path: String) throws -> File do { let

    file = try loadFile(at: path) // execute using `file` } catch FileError.noSuchFile { // Error handling when there is no file } catch FileError.permissionDenied { // Error handling when there is no permission } catch { // Error handling in other cases }
  20. 3. Universal error • ϓϩάϥϜ͕ఀࢭ͢Ε͹͍͍ɺΤϥʔॲཧΛ͢Δඞཁ͸ͳ͍ͱߟ͑Δ৔ ߹ʹ࠾༻ • fail fast: ߈ܸతϓϩάϥϛϯά,

    ૣΊʹΫϥογϡͤ͞Δ • த్൒୺ʹಈ͍͍ͯΔΑΓࢮΜͩϓϩάϥϜͷํ͕μϝʔδ͕গͳ͍ • ݎ࿚ੑΑΓਖ਼౰ੑΛॏࢹ͢Δέʔεʹ͓͍ͯ࢖༻͢Δ(ෆਖ਼֬ͳ݁Ռ Λฦ͞ͳ͍͜ͱ) • ճ෮ෆೳ • ex. ϝϞϦෆ଍΍ελοΫΦʔόʔϑϩʔɺϓϥάϥϜ্هड़͕ඞཁͩ ͕ى͜Γಘͳ͍έʔε
  21. https://speakerdeck.com/twada/php-conference-2016-revised

  22. required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }

    func doSomething(s: String) { switch s { case "a": // ... case "b": // ... default: // unreachable fatalError("The impossible occurred") } }
  23. 4. Logic failure • ex. ͦͷҾ਺͕དྷΔ͜ͱࣗମ͕όάɺίʔυΛमਖ਼͢Δඞཁ͕͋ Δͱߟ͑Δ৔߹ʹ࠾༻ • ࣮ߦ࣌ʹىͬͯ͜͸͍͚ͳ͍ͷͰɺىͬͨ͜৔߹ͷڍಈ͸ະఆٛ •

    ࠷దԽ → ࣮ߦ࣌ͷ҆શੑνΣοΫ͸ߦΘͳ͍ → Φʔόʔϔο υΛ཈͑Δ • ex. nil͕ೖ͍ͬͯΔOptionalʹରͯ͠ Forced unwrapping, ഑ྻ ͷଘࡏ͠ͳ͍ΠϯσοΫε΁ͷΞΫηεͳͲ
  24. func printEvenSquare(_ i: Int) { precondition(i % 2 == 0)

    print("even: \(i * i)") } printEvenSquare(5) // crash if odd number
  25. assert/precondition/fatalError • -Onone: Compile without any optimization == debug build

    • -O: with optimizations == release build • -Ounchecked: Compile with optimizations and remove runtime safety checks http://safx-dev.blogspot.jp/2015/02/swift-assert-precondition-and-fatalerror.html
  26. Τϥʔͷ಺༰ʹΑͬͯ෼ྨ͢ΔͷͰ͸ͳ͘ɺ Τϥʔʹ”ͲͷΑ͏ʹରԠ͍͔ͤͨ͞” Ͱ෼ྨ͢Δ

  27. “ྫ֎͸ɺͦͷ໊͕ࣔ͢௨Γɺྫ֎త ৚݅ʹରͯ͠ͷΈ࢖༻͢Δ΂͖Ͱ͢ɻ ௨ৗͷ੍ޚϑϩʔʹରͯ͠͸ɺܾͯ͠ ࢖༻͢΂͖Ͱ͸͋Γ·ͤΜɻ”

  28. http://xn--97-273ae6a4irb6e2hsoiozc2g4b8082p.com/ %E3%82%A8%E3%83%83%E3%82%BB%E3%82%A4/%E8%A6%8B%E7%9F%A5%E3%82%89%E3%81%AC%E4%BA%BA %E3%81%A8%E3%82%82%E3%81%86%E3%81%BE%E3%81%8F%E3%82%84%E3%82%8B%E3%81%AB%E3%81%AFs “ग़དྷͯ͸ͳΒ͵͜ͱΛग़དྷͳ͘͢Δ ʹ͸ɺʮग़དྷͯ͸ͳΒ͵͜ͱΛې͡Δʯ ͷͰ͸ͳ͘ɺ͸͡Ί͔Βʮग़དྷ͍͍ͯ ͜ͱ͚ͩΛग़དྷΔΑ͏ʹ͢Δʯͱߟ͑ ΔͷͰ͢ɻ”

  29. Conclusion • ྫ֎(Τϥʔ)ػߏΛ࢖͏໨త͸ɺແࢹͰ͖ͳ͍ํ๏Ͱঢ়ଶΛ஌ΒͤΔ͜ͱ • “ͲͷΑ͏ʹରԠ͍͔ͤͨ͞”ͰɺΤϥʔΛ෼ྨ͢Δ • લఏ৚͕݅໌֬ → Simple domain

    error • ݪҼ͝ͱʹରԠ → Recoverable error • ճ෮ෆೳ → Universal error • ίʔυͷޡΓ → Logic failure • Τϥʔ෼ྨ͕ద੾Ͱ͋Ε͹ɺடং͕อͨΕΔ͸ͣ
  30. See also • ྫ֎ઃܭʹ͓͚Δେࡑ http://www.slideshare.net/t_wada/exception-design-by-contract • ྫ֎ೖ໳Ҏલ http://qiita.com/Kokudori/items/3a953c00012408f76ab9 • Java/Androidʹ͓͚Δྫ֎ઃܭɺ͋Δ͍͸ʮܖ໿ʹΑΔઃܭʯʹΑΔγϯϓϧ͞ͷ௥ٻ

    http://qiita.com/yuya_presto/items/3b651d6b0cf38f77e933 • ྫ֎ઃܭͷ࿩ https://gist.github.com/sunaot/6138546 • ͳͥGoݴޠ(golang)͸Α͍ݴޠͳͷ͔ɾGoͰϓϩάϥϜΛॻ͘΂͖ͳͷ͔ http:// www.yunabe.jp/docs/why_golang_is_good.html • Three Stories about Error Handling in Swift http://koherent.org/tryswift/codes.html
  31. Any Questions?