Python 2- und 3-kompatibles Exception-Handling

Code-Stücke können hier veröffentlicht werden.
Antworten
Dauerbaustelle
User
Beiträge: 996
Registriert: Mittwoch 9. Januar 2008, 13:48

Edit: Mist. Hier die korrigierte Version http://paste.pocoo.org/show/142848/

Kann man dann so verwenden:
http://paste.pocoo.org/show/142836/

Code: Alles auswählen

>>> handler
<__main__.PyExceptionHandler object at 0xb7d4de2c>
>>> with handler.catch():
...     1+2
... 
3
>>> handler.exception
>>> 
>>> with handler.catch():
...     int('a')
... 
>>> handler.exception
ValueError("invalid literal for int() with base 10: 'a'",)
>>> 
>>> with handler.catch(TypeError, RuntimeError):
...     int('a')
... 
Traceback (most recent call last):
  File "<console>", line 2, in <module>
ValueError: invalid literal for int() with base 10: 'a'
Antworten