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

Mario Corchero - How to properly raise, handle and create them.

Mario Corchero - How to properly raise, handle and create them.

Did you know there are multiple ways to raise and capture exceptions? Have you ever wondered if you should raise a built-in exception or create your own hierarchy? Did you ever find it hard to understand what an exception meant?

This talk will go through the decisions needed to raise and capture exceptions when creating a library. We will look at how to translate and handle errors, create your own exceptions, and make exceptions clear and easy to troubleshoot, while also understanding how they actually work, common pitfalls.

https://us.pycon.org/2019/schedule/presentation/225/

PyCon 2019

May 04, 2019
Tweet

More Decks by PyCon 2019

Other Decks in Programming

Transcript

  1. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Exceptional
    Exceptions
    How to properly raise, handle and create them
    PyCon 2019
    Mario Corchero
    @mariocj89

    View Slide

  2. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Let’s just raise an exception, right?

    View Slide

  3. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Let’s just raise an exception, right?

    View Slide

  4. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Let’s just raise an exception, right?

    View Slide

  5. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Let’s just raise an exception, right?

    View Slide

  6. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Let’s just raise an exception, right?

    View Slide

  7. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Let’s just raise an exception, right?

    View Slide

  8. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Let’s go step by step

    View Slide

  9. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    How to capture an exception

    View Slide

  10. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    What can go in the except?

    View Slide

  11. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    What can go in the except?

    View Slide

  12. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Using finally

    View Slide

  13. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Using else

    View Slide

  14. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Order of execution?

    View Slide

  15. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    What about returning in the middle?

    View Slide

  16. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Logging exceptions
    Use exc_info to include exception information

    View Slide

  17. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Logging exceptions

    View Slide

  18. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Logging exceptions
    Errors should never pass silently.
    Unless explicitly silenced.

    View Slide

  19. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Logging exceptions

    View Slide

  20. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Logging exceptions

    View Slide

  21. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Scoping of the except
    See PEP 3110.
    In short: exception -> traceback -> stack frame -> exception

    View Slide

  22. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Raising exceptions

    View Slide

  23. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Raising exceptions

    View Slide

  24. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Raising exceptions

    View Slide

  25. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Raise it!

    View Slide

  26. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    The tricky raise within except

    View Slide

  27. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    The tricky, raise within except

    View Slide

  28. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Reraise

    View Slide

  29. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Reraise

    View Slide

  30. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Reraise

    View Slide

  31. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Reraise

    View Slide

  32. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Chaining exceptions

    View Slide

  33. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Chaining exceptions

    View Slide

  34. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Eliding previous exception

    View Slide

  35. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Eliding previous exception

    View Slide

  36. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    False friends: NotImplemented
    Passes without printing.
    37,762 hits in GitHub as of Apr 2019.

    View Slide

  37. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Raise on a thread?

    View Slide

  38. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Raise on a thread?

    View Slide

  39. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Creating exceptions

    View Slide

  40. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Creating exceptions

    View Slide

  41. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    The exception hierarchy Check https://docs.python.org/3/library/exceptions.html

    View Slide

  42. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Exception attributes

    View Slide

  43. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Exception attributes

    View Slide

  44. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Exception attributes

    View Slide

  45. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Creating new classes

    View Slide

  46. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Creating new classes

    View Slide

  47. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Exception hierarchy

    View Slide

  48. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Multiple inheritance
    • Decimal:
    • Requests:

    View Slide

  49. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Can I use Error codes?

    View Slide

  50. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Conclusions

    View Slide

  51. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Q&A
    Ask me about travelling to Extremadura!

    View Slide

  52. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Bonus!

    View Slide

  53. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Bonus!

    View Slide

  54. © 2018 Bloomberg Finance L.P. All rights reserved.
    © 2019 Bloomberg Finance L.P. All rights reserved.
    Bonus!

    View Slide