<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def dumps(messages):
 """ Serialize messages to a JSON formatted str Args: messages (list):
 The list of messages to serialize. Each message in the messages is subclass of Messge. Returns: str: Serialized messages. Raises: TypeError: If at least one message is not instance of Message class or subclass. """

    serialized_messages = []
    try:
        for message in messages:
            message_dict = message._dump()
            serialized_messages.append(message_dict)
    except AttributeError:
        _log.error("Improper object for messages serialization.")
        raise TypeError("Message have to be instance of Message class or subclass.")

    return json.dumps(serialized_messages, sort_keys=True)