import functools


def return_on_error(val):
    """Decorator that makes a function return a default value instead of throwing an exception

    Example:

    @return_on_error("green")
    def foo(x):
        if x:
            return "red"
        else:
            raise Exception("This will be caught and ignored")

    foo(True)  # returns "red"
    foo(False) # returns "green"

    """
    def val_decorator(func):
        @functools.wraps(func)
        def func_wrapper(*args, **kwargs):
            try:
                return func(*args, **kwargs)
            except:
                return val
        return func_wrapper
    return val_decorator