diff --git a/pymaybe/__init__.py b/pymaybe/__init__.py index d98a919..b047338 100755 --- a/pymaybe/__init__.py +++ b/pymaybe/__init__.py @@ -123,7 +123,10 @@ def __init__(self, value): self.__value = value def __call__(self, *args, **kwargs): - return maybe(self.__value(*args, **kwargs)) + try: + return maybe(self.__value(*args, **kwargs)) + except Exception: + return Nothing() # region Comparison def __cmp__(self, other): diff --git a/tests/test_pymaybe.py b/tests/test_pymaybe.py index 37d7809..d997d76 100755 --- a/tests/test_pymaybe.py +++ b/tests/test_pymaybe.py @@ -367,6 +367,13 @@ def test_something_forwardsMethodCalls_handlesNonExisting(self): result = maybe('VALUE').lowerr() assert result.is_none() + def test_something_forwardsMethodCalls_handlesException(self): + class Thrower(): + def throw(self): + raise Exception() + result = maybe(Thrower()).throw() + assert result.is_none() + def test_nothing_forwardsMethodCalls_handlesNonExisting(self): result = maybe(None).invalid().call() assert result.is_none()