def convert_exception_to(to_type, with_message=False):
 (etype, original, tb) = sys.exc_info()
 try:
  if (with_message is False):
   message = None
  elif (with_message is True):
   message = str(original)
  else:
   message = str(with_message)
  raise to_type, message, tb
 except to_type as e:
  return e

def convert_exception_to ( to_type , with_message = False ) : ( etype , original , tb ) = sys . exc_info ( ) try : if ( with_message is False ) : message = None elif ( with_message is True ) : message = str ( original ) else : message = str ( with_message ) raise to_type , message , tb except to_type as e : return e
should only be called from an exception handler .