class LexicalError(ValueError): pass def scan(): pos = 14 char = '@' state = 'in-number' error_msg = 'unexpected character {} in state {} at pos {}' raise LexicalError(error_msg.format(char, state, pos)) try: scan() except LexicalError as e: print(e)