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

Mario Corchero - How to properly raise, handle ...

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
  2. © 2018 Bloomberg Finance L.P. All rights reserved. © 2019

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

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

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

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

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

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

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

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

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

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

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

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

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

    Bloomberg Finance L.P. All rights reserved. What about returning in the middle?
  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
  17. © 2018 Bloomberg Finance L.P. All rights reserved. © 2019

    Bloomberg Finance L.P. All rights reserved. Logging exceptions
  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.
  19. © 2018 Bloomberg Finance L.P. All rights reserved. © 2019

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

    Bloomberg Finance L.P. All rights reserved. Logging exceptions
  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
  22. © 2018 Bloomberg Finance L.P. All rights reserved. © 2019

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

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

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

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

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

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

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

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

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

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

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

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

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

    Bloomberg Finance L.P. All rights reserved. Eliding previous exception
  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.
  37. © 2018 Bloomberg Finance L.P. All rights reserved. © 2019

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

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

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

    Bloomberg Finance L.P. All rights reserved. Creating exceptions
  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
  42. © 2018 Bloomberg Finance L.P. All rights reserved. © 2019

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

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

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

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

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

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

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

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

    Bloomberg Finance L.P. All rights reserved. Conclusions
  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!
  52. © 2018 Bloomberg Finance L.P. All rights reserved. © 2019

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

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

    Bloomberg Finance L.P. All rights reserved. Bonus!