Handle specific exception from python package
Question:
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.
Answers:
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.
check the inputs provided by you in the formula.
you must have entered the wrong value of strike price
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.
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.
check the inputs provided by you in the formula.
you must have entered the wrong value of strike price