exception core.customexception.CustomException(*args, **kargs)[source]

Bases: exceptions.Exception

CustomException is an extension of builtin Exception. Goal is to provide an unique structure for all kind of exceptions to allow high-level exception handling.

For this reason, CustomException always provides :
  • a title (getTitle)
  • a message (getMessage)
  • a detailed description (getDesc)

Typicall use is :

def print_error(error):
"number of error arguments has no importance" print_in_red(error.getTitle()) print_in_blue(error.getMessage())
try :
action
except CustomExceptionNumberOne,error :
print_error(error)
except CustomExceptionNumberTwo,error :
print_error(error)
except CustomExceptionNumberThree,error :
fix the problem

# CustomExceptionNumberFour isn't intercepted here but for example in GUI classes else :

print 'done'

If CustomException takes one argument, you can reach it using "%(value)s". If it takes more than one, you must reimplement _kargs method.

Example :

class ErrNotMatchingPuzzlePiece(CustomException):
  title = u'Error: puzzle pieces do not match'
  message = u'%(compass_1)s part of piece %(piece_1)%s do not match %(compass_2)s part of piece %(piece_2)%s'
  desc = "This error is raised because ..."

  def _kargs(self):
    return dict(
      compass_1=unicode(self._args[0].num),
      piece_1=self._args[1],
      compass_2=unicode(self._args[2].num),
      piece_2=self._args[3],
      )

#two raise this exception:
raise ErrNotMatchingPuzzlePiece(p1, u'North', p2, u'South')
# with p1 and p2 two "Piece" objects containing a "num" attribute.
getDesc()[source]
getMessage()[source]
getTitle()[source]
kargs()[source]
rkargs()[source]
desc = u'No details available'
message = u'An unknown error occurs'
title = u'Unknown Error'
exception core.customexception.UserException(*args, **kargs)[source]

Bases: core.customexception.CustomException

core.customexception.cast_error(error, klass, title=True, message=True, desc=True, **kargs)[source]
core.customexception.display_error_color(e)[source]

Previous topic

<no title>

Next topic

<no title>

This Page