def try_int(val):
    """Tries to convert val to int.

    Raises ValueError upon failure.
    In contrast to builtin.int this function returns 0 for an empty string.
    """
    try:
        return int(val)
    except ValueError:
        if len(val) == 0:
            return 0
        else:
            raise ValueError("Cannot convert to integer: {}".format(val))