Přejít na menu

Exceptions in Python

Správa článků

Vyhledávání Vyhledávání
16.8.2013 08:32
,
Počet přečtení: 603

Custom exceptions and re-raising.

Custom attribute of custom exception:

class MyException(Exception):

def __init__(self, message):
self.msg = message

def __str__(self):
return repr(self.msg)

Exception re-raising

Simple solution:

from sys import exc_info
def reraise(e, additional_message):
raise type(e), type(e)("%s \n CLARIFICATION: %s" % (e.args[1], additional_message)), exc_info()[2]
#enddef

# example call

try:
50 / 0
except ZeroDivisionError as e:
reraise(e, "Division by zero is not allowed!")

Vytvořil 16. srpna 2013 v 08:41:56 mira. Upravováno 1x, naposledy 21. dubna 2014 ve 20:49:42, mira


Diskuze ke článku

Vložení nového komentáře
*
*
*