Skip to content
Advertisement

Handle specific exception from python package

I would like to handle the following Exception from py_vollib/py_lets_be_rational in specific way.

py_lets_be_rational.exceptions.BelowIntrinsicException: The volatility is below the intrinsic value.

Tried this without success:

from py_vollib.black.implied_volatility import implied_volatility as impl_vol_b
from py_lets_be_rational.exceptions import BelowIntrinsicException

try:
    call_vol = impl_vol_b(discounted_option_price, F, K, r, t, type)
except BelowIntrinsicException as e:
    if str(e) != 'The volatility is below the intrinsic value':
        raise
    else:
        call_vol = 0

what am I doing wrong? any help would be appreciated.

Advertisement

Answer

Looking at the implementation, you’re missing the period at the end of the sentence:

if str(e) != 'The volatility is below the intrinsic value.':

I don’t see the point in this check because that will always be the message the exception is created with.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement