def check_type_value(val, name, expected_type, allow_none=False, print_value=True, none_msg=''):
    """
    Check if the given value is of expected type. And also check if the val is None.

    :param val: the given value to check
    :param name: name of val
    :param expected_type: the expected type
    :param allow_none: whether the val is allowed to be None
    :param print_value: whether or not to print the value name in case of error
    :param location: The location of the potential hdf5 value to check
    :raise TypeError: if val is not of expected type
    :raise ValueError: if val is None while not allow None
    """
    message = name

    if print_value:
        message += ' of value ' + str(val)

    if val is None and not allow_none:
        raise ValueError(message + ' should not be None.' + none_msg)
    if not isinstance(val, expected_type):
        raise TypeError(message  + ' should be of type ' + str(expected_type) + '.' + ' but is of type ' + type(val).__name__)

    return val