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

Exceptional Exceptions - EuroPython 2019

Exceptional Exceptions - EuroPython 2019

Mario Corchero

July 11, 2019
Tweet

More Decks by Mario Corchero

Other Decks in Programming

Transcript

  1. © 2019 Bloomberg Finance L.P. All rights reserved. Exceptional Exceptions:

    How to properly raise, handle and create them EuroPython 2019 July 11, 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 go step by step
  5. © 2018 Bloomberg Finance L.P. All rights reserved. © 2019

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

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

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

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

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

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

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

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

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

    Bloomberg Finance L.P. All rights reserved. Logging exceptions But... When I have...
  17. © 2018 Bloomberg Finance L.P. All rights reserved. © 2019

    Bloomberg Finance L.P. All rights reserved. Logging exceptions But... What if I know...
  18. © 2018 Bloomberg Finance L.P. All rights reserved. © 2019

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

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

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

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

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

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

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

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

    Bloomberg Finance L.P. All rights reserved. Raise on a thread?
  32. © 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
  33. © 2018 Bloomberg Finance L.P. All rights reserved. © 2019

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

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

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

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

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

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

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

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

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

    Bloomberg Finance L.P. All rights reserved. Extra content!
  43. © 2018 Bloomberg Finance L.P. All rights reserved. © 2019

    Bloomberg Finance L.P. All rights reserved. New magic in 3.8
  44. © 2018 Bloomberg Finance L.P. All rights reserved. © 2019

    Bloomberg Finance L.P. All rights reserved. New magic in 3.8
  45. © 2018 Bloomberg Finance L.P. All rights reserved. © 2019

    Bloomberg Finance L.P. All rights reserved. Returns in finally
  46. © 2018 Bloomberg Finance L.P. All rights reserved. © 2019

    Bloomberg Finance L.P. All rights reserved. Returns in finally
  47. © 2018 Bloomberg Finance L.P. All rights reserved. © 2019

    Bloomberg Finance L.P. All rights reserved. Returns in finally
  48. © 2019 Bloomberg Finance L.P. All rights reserved. Thank you!

    Questions?? Also, ask me about travelling to Extremadura!