Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
© 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
Slide 2
Slide 2 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Let’s just raise an exception, right?
Slide 3
Slide 3 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Let’s just raise an exception, right?
Slide 4
Slide 4 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Let’s just raise an exception, right?
Slide 5
Slide 5 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Let’s just raise an exception, right?
Slide 6
Slide 6 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Let’s just raise an exception, right?
Slide 7
Slide 7 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Let’s just raise an exception, right?
Slide 8
Slide 8 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Let’s go step by step
Slide 9
Slide 9 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. How to capture an exception
Slide 10
Slide 10 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. What can go in the except?
Slide 11
Slide 11 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. What can go in the except?
Slide 12
Slide 12 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Using finally
Slide 13
Slide 13 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Using else
Slide 14
Slide 14 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Order of execution?
Slide 15
Slide 15 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. What about returning in the middle?
Slide 16
Slide 16 text
© 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
Slide 17
Slide 17 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Logging exceptions
Slide 18
Slide 18 text
© 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.
Slide 19
Slide 19 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Logging exceptions
Slide 20
Slide 20 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Logging exceptions
Slide 21
Slide 21 text
© 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
Slide 22
Slide 22 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Raising exceptions
Slide 23
Slide 23 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Raising exceptions
Slide 24
Slide 24 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Raising exceptions
Slide 25
Slide 25 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Raise it!
Slide 26
Slide 26 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. The tricky raise within except
Slide 27
Slide 27 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. The tricky, raise within except
Slide 28
Slide 28 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Reraise
Slide 29
Slide 29 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Reraise
Slide 30
Slide 30 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Reraise
Slide 31
Slide 31 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Reraise
Slide 32
Slide 32 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Chaining exceptions
Slide 33
Slide 33 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Chaining exceptions
Slide 34
Slide 34 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Eliding previous exception
Slide 35
Slide 35 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Eliding previous exception
Slide 36
Slide 36 text
© 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.
Slide 37
Slide 37 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Raise on a thread?
Slide 38
Slide 38 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Raise on a thread?
Slide 39
Slide 39 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Creating exceptions
Slide 40
Slide 40 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Creating exceptions
Slide 41
Slide 41 text
© 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
Slide 42
Slide 42 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Exception attributes
Slide 43
Slide 43 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Exception attributes
Slide 44
Slide 44 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Exception attributes
Slide 45
Slide 45 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Creating new classes
Slide 46
Slide 46 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Creating new classes
Slide 47
Slide 47 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Exception hierarchy
Slide 48
Slide 48 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Multiple inheritance • Decimal: • Requests:
Slide 49
Slide 49 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Can I use Error codes?
Slide 50
Slide 50 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Conclusions
Slide 51
Slide 51 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Q&A Ask me about travelling to Extremadura!
Slide 52
Slide 52 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Bonus!
Slide 53
Slide 53 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Bonus!
Slide 54
Slide 54 text
© 2018 Bloomberg Finance L.P. All rights reserved. © 2019 Bloomberg Finance L.P. All rights reserved. Bonus!